Page 1 of 1
How to use Bash to bulk-edit filenames?
Posted: Thu 06 Dec 2012, 03:52
by RSH
Hi.
I have a list like this one:
Code: Select all
LP2_AbiWord-2.8.6.sfs
LP2_AFI-1.0.3.sfs
...
LP2_Aqualung09b11.sfs
LP2_Ardour286.sfs
...
LP2_AsUnder_2.1.sfs
LP2_AutoAdjustPhoto_GUI-1.0.1.sfs
...
...
...
I want to remove the "LP2_" prefix and everything except the name, like the "09b11.sfs" from Aqualung, the "-1.0.1.sfs" from AutoAdjustPhoto, the "_2.1.sfs" from AsUnder and the "-2.8.6.sfs" from AbiWord.
Could this be done using a single command like
sed?
If, how?
Or will the different endings:" -1.0.1.sfs" - "_2.1.sfs" - "09b11.sfs" cause problems?
Thanks
RSH
Edit: I want to do this using bash.
Posted: Thu 06 Dec 2012, 11:41
by jamesbond
The LP2 prefix is easy.
Assuming that stuff is stored in a file called input.txt (one for each line), you can do either:
Code: Select all
#!/bin/sh
while read p; do
echo ${p#LP2_}
done < input.txt > output.txt
or
Code: Select all
sed 's/^LP2_//' input.txt > output.txt
The suffix, however, is difficult, because there is no way to know a certain word is part of the suffix to be erased, or it is part of the name you want to keep, so you'll have to use a marker that is unique to separate between the words you want to keep and the suffix you want to delete. It can the the last underscore (_), last minus (-), or whatever.
I will assume here that you've decided that all the suffix will be preceded by an underscore; that is, everything after the last underscore will be deleted.
Code: Select all
#!/bin/sh
while read p; do
echo ${p%_*}
done < input.txt > output.txt
or
Code: Select all
sed 's/_[^_]*$//' input.txt > output.txt
Re: How to remove parts from filename?
Posted: Thu 06 Dec 2012, 11:49
by Geoffrey
RSH wrote:Hi.
I have a list like this one:
Code: Select all
LP2_AbiWord-2.8.6.sfs
LP2_AFI-1.0.3.sfs
...
LP2_Aqualung09b11.sfs
LP2_Ardour286.sfs
...
LP2_AsUnder_2.1.sfs
LP2_AutoAdjustPhoto_GUI-1.0.1.sfs
...
...
...
I want to remove the "LP2_" prefix and everything except the name, like the "09b11.sfs" from Aqualung, the "-1.0.1.sfs" from AutoAdjustPhoto, the "_2.1.sfs" from AsUnder and the "-2.8.6.sfs" from AbiWord.
Could this be done using a single command like
sed?
If, how?
Or will the different endings:" -1.0.1.sfs" - "_2.1.sfs" - "09b11.sfs" cause problems?
Thanks
RSH
Edit: I want to do this using bash.
Will something like this help
Code: Select all
#!/bin/bash
sed -r 's/[_-0123456789]+/ /g' ./list1 > ./list2
awk '{ print $2 }' < ./list2 > ./list3
sed '/^ *$/d' < ./list3 > ./list4
input list
Code: Select all
LP2_AbiWord-2.8.6.sfs
LP2_AFI-1.0.3.sfs
...
LP2_Aqualung09b11.sfs
LP2_Ardour286.sfs
...
LP2_AsUnder_2.1.sfs
LP2_AutoAdjustPhoto_GUI-1.0.1.sfs
...
...
...
output list
Code: Select all
AbiWord
AFI
Aqualung
Ardour
AsUnder
AutoAdjustPhoto
Posted: Thu 06 Dec 2012, 12:29
by RSH
Thanks to both of you,
jamesbond and
Geoffrey!
Will something like this help
Code: Select all
#!/bin/bash
sed -r 's/[_-0123456789]+/ /g' ./list1 > ./list2
awk '{ print $2 }' < ./list2 > ./list3
sed '/^ *$/d' < ./list3 > ./list4
Yes, this is great help! Thanks.
It doesn't work on some special names like
LP2_SAG_CAD-0.9.14.sfs, which returns
SAG only instead of
SAG_CAD. Never mind, I can refine these special cases manually.
It is much better than to refine the complete list (272 lines) manually (several times).
Thanks