Reputation: 1137
Good day.
I actually have 2 questions that are related to the sed command in shell and they are very similar.
The first question is how to use sed to get a file name and remover part of it's name like the example below:
Original file:
BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
What i want the file name to look like:
BAT_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
I just want the "MAN" part after the first underline to be removed out of the original file name.
The second question is about the following sed command that I've found on a file a while ago:
random_string_var_name=$(echo $file_name | sed -r 's/^[^_]*_[^_]*_(.*_t[0-9]{1}).*(_[0-9]*)\.txt/_\1\2/')
this pretty much get parts of a file name a saves it on a variable, like the example bellow:
Name of the file:
BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
What that sed command gets:
T_spades_proc_whatever_t6_12345
I got what it does but i don't understand how that command works, so i would like to understand that.
Upvotes: 1
Views: 4793
Reputation: 11573
I just want the "MAN" part after the first underline to be removed out of the original file name.
echo "BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt" | sed "s/MAN_//"
What if i want to always remove the first word after the first underline and keep everything else?
echo "BAT_MAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt" | sed -r 's/^([^_]*)_[^_]*(_.*)/\1\2/'
what does this do:
echo $file_name | sed -r 's/^[^_]*_[^_]*_(.*_t[0-9]{1}).*(_[0-9]*)\.txt/_\1\2/')
-r
: runs sed in "extended regex" mode^
: matches beginning of word[^_]*
matches everything except underline 0 or more times_
matches underline(.*_t[0-9]{1})
matches zero or more of anything followed by _t and only one number. This match is stored in variable 1(_[0-9]*)
same thing, only that there is no prefix/_\1\2
: replaces the whole filename with _ at the beginning and the match in the first brackets and the match in the second bracketI recommend reading up on regular expressions. They are important and not really hard to get into
Upvotes: 3
Reputation: 464
I think that you may have something else than "MAN", you may have "WOMAN". So you can use:
file_name=BAT_WOMAN_T_spades_proc_whatever_t6_12345_14785963214785_12345.txt
echo $file_name | sed 's/_[^_]*_/_/'
Upvotes: 1