Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 21 Oct 2014, 06:48
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to use Bash to bulk-edit filenames?
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [4 Posts]  
Author Message
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: 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.

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
jamesbond

Joined: 26 Feb 2007
Posts: 2226
Location: The Blue Marble

PostPosted: 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.
Contributed Fatdog64 packages thread
Back to top
View user's profile Send private message 
Geoffrey


Joined: 30 May 2010
Posts: 1428
Location: Queensland Australia ɹǝpu∩uʍop

PostPosted: Thu 06 Dec 2012, 07:49    Post subject: Re: How to remove parts from filename?
Subject description: 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
createlist.tar.gz
Description 
gz

 Download 
Filename  createlist.tar.gz 
Filesize  374 Bytes 
Downloaded  261 Time(s) 

_________________
Carolina: Recent Repository Additions
Create strikethrough text HERE
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2420
Location: Germany

PostPosted: 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

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [4 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0606s ][ Queries: 13 (0.0056s) ][ GZIP on ]