The time now is Wed 19 Jun 2013, 02:09
All times are UTC - 4 |
| Author |
Message |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Wed 05 Dec 2012, 23:52 Post_subject:
How to use Bash to bulk-edit filenames? |
|
Hi.
I have a list like this one:
| Code: | 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.
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
jamesbond
Joined: 26 Feb 2007 Posts: 1573 Location: The Blue Marble
|
Posted: Thu 06 Dec 2012, 07:41 Post_subject:
|
|
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: | #!/bin/sh
while read p; do
echo ${p#LP2_}
done < input.txt > output.txt |
or
| Code: | | 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: | #!/bin/sh
while read p; do
echo ${p%_*}
done < input.txt > output.txt | or
| Code: | | sed 's/_[^_]*$//' input.txt > output.txt |
_________________ Fatdog64, Slacko and Puppeee user. Puppy user since 2.13
|
|
Back to top
|
|
 |
Geoffrey

Joined: 30 May 2010 Posts: 935 Location: Queensland Australia ɹǝpu∩uʍop
|
Posted: Thu 06 Dec 2012, 07:49 Post_subject:
Re: How to remove parts from filename? Sub_title: How to do this using bash |
|
| RSH wrote: | Hi.
I have a list like this one:
| Code: | 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: | #!/bin/bash
sed -r 's/[_-0123456789]+/ /g' ./list1 > ./list2
awk '{ print $2 }' < ./list2 > ./list3
sed '/^ *$/d' < ./list3 > ./list4 |
input list
| Code: | 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: | AbiWord
AFI
Aqualung
Ardour
AsUnder
AutoAdjustPhoto
|
| Description |
|

Download |
| Filename |
createlist.tar.gz |
| Filesize |
374 Bytes |
| Downloaded |
110 Time(s) |
_________________ AdobeAIR App Links: HERE
Carolina: Recent Repository Additions
|
|
Back to top
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Thu 06 Dec 2012, 08:29 Post_subject:
|
|
Thanks to both of you, jamesbond and Geoffrey!
| Quote: | Will something like this help
| Code: | #!/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
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
|
|
|
Rules_post_cannot Rules_reply_cannot Rules_edit_cannot Rules_delete_cannot Rules_vote_cannot You cannot attach files in this forum You can download files in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|