How can I remove the filename prefix in Bash as in the following example: XY TD-11212239.pdf to get 11212239.pdf i.e, remove XY TD-? How to add prefix/suffix to each line of a file? The most readable and easy to remember one for me to add both prefix and suffix is the following. How to change the color of particular file type in bash shell in linux? Parameter substitution and/or expansion. Remove from $var the shortest part of $Pattern that matches the front end of. #!/bin/bash # rfe.sh: Renaming file extensions. # # rfe old_extension new_extension.
I need to remove the prefix/suffix from the original string. bash - remove a fixed prefix/suffix from a string. mkdir says that file exists when it doesn't. Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.
How to remove filename prefix with an Posix shell. You have given very little information, but assuming you are doing this in bash, and have a list of files whose prefix needs to be removed, you can pipe the list through sed: For example./generate_your_list_of_files | sed - e 's/^prefix//'.
TD- file. 1 TD- file. TD- file. 3- beep | sed - e 's/\< TD- //g'.
The first one matches prefix in the beginning of the line and removes it. The second one matches TD- (or any other prefix you want to substitute) only when it happens at the beginning of a word and replaces it in all the matches in all the lines. This could get dangerous though, for example a file like TD- file\ that\ TD- contains\ space. As a side note, don't get your list of files using ls. That's a horrible mistake.
If you need to get a list of files to work with and refer to in more than a single place, I'd suggest putting them in an array: files=(*). Due to popular requests (the single comment below), here is how to rename all files in the directory that start with TD- so that the prefix is removed (Thanks to @tripleee): for file in prefix*.
Leave a Reply.
Write something about yourself. No need to be fancy, just an overview.