How to use Bash to bulk-edit filenames?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

How to use Bash to bulk-edit filenames?

#1 Post 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.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#2 Post 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
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
Geoffrey
Posts: 2355
Joined: Sun 30 May 2010, 08:42
Location: Queensland

Re: How to remove parts from filename?

#3 Post 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
Attachments
createlist.tar.gz
(374 Bytes) Downloaded 420 times
[b]Carolina:[/b] [url=http://smokey01.com/carolina/pages/recent-repo.html]Recent Repository Additions[/url]
[img]https://dl.dropboxusercontent.com/s/ahfade8q4def1lq/signbot.gif[/img]

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#4 Post 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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

Post Reply