Renaming by manipulating strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming by manipulating strings
# 1  
Old 10-21-2012
Renaming by manipulating strings

hello

does someone want to help me for this one ?


i want to rename file by inversing parts of filenames separated by the delimiter "--"



sometimes filenames have three strings :
Code:
aabb4ccdd eeffgg -- 5566 -- aa78bb ccd eef gghhi.ext

to
Code:
aa78bb ccd eef gghhi -- 5566 -- aabb4ccdd eeffgg.ext

Code:
aabbcc -- 55 -- aab667ccdd ee.ext

to
Code:
aab667ccdd ee -- 55 -- aabbcc.ext

Code:
556aa -- aabb -- 7788.ext

to
Code:
7788 -- aabb -- 556aa.ext

sometimes there are only two strings :
Code:
aabbccdd eef -- gghh iijjkkl.ext

to
Code:
gghh iijjkkl -- aabbccdd eef.ext

when there are two strings i think the solution is to inverse strings separated by "--"



i've got filename like these one too, i'd like to put the digits at at the end to the beginning :
Code:
aabbccdde -- ffgghh iijjkkll5566778.ext

to
Code:
5566778 aabbccdde -- ffgghh iijjkkll.ext

Code:
aab55c -- ddeeffgghh5566.ext

to
Code:
5566 aab55c -- ddeeffgghh.ext

thanks for you help !

Last edited by mc2z674gj; 10-21-2012 at 11:37 AM.. Reason: code tags
# 2  
Old 10-21-2012
for above two case try this...

Code:
for i in *.ext
do
file_name=$(echo "$i" | awk -F " -- " '{split($NF,a,".");s=$1;$1=a[1];$(NF)=s"."a[2]}1' OFS=" -- ")
mv $i $file_name
done

for last having numeric at the end of file..

Code:
for i in *[0-9].ext
do
file=$(echo "$i" | awk -F "" '{for(i=1;i<=NF;i++){if($i ~ /[A-z]/){s=""}else{if($i == "."){print s}else{s=s?s""$i:$i}}}}')
file_name=$(echo "$i" | awk -v RPL="$file" '{sub(RPL,"",$0);print RPL,$0}')
mv $i $file_name
done


Last edited by pamu; 10-21-2012 at 06:05 AM..
This User Gave Thanks to pamu For This Post:
# 3  
Old 10-21-2012
1>Verify is these are the conditions you require by just printing the mv command
Code:
ls *ext | perl -F"--" -alne '{if($#F==1){
if($F[1]=~/(\d+).ext/){ $p=$1;$F[0]=~s/^/$p /g;$F[1]=~s/$p//;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
else { ($F[0],$F[1])=($F[1],$F[0]);$F[0]=~s/.ext//;$F[1]=~s/ *$/(.ext|^ *)/;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
}else{ ($F[0],$F[2])=($F[2],$F[0]);$F[0]=~s/.ext//;$F[2]=~s/ *$/(.ext|^ *)/;;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
}'

2>Once you are sure of what you want execute it
Code:
ls *ext | perl -F"--" -alne '{if($#F==1){
if($F[1]=~/(\d+).ext/){ $p=$1;$F[0]=~s/^/$p /g;$F[1]=~s/$p//;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
else { ($F[0],$F[1])=($F[1],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[1]=~s/ *$/.ext/;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
}else{ ($F[0],$F[2])=($F[2],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[2]=~s/ *$/.ext/;;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;}
}' | sh


Last edited by msabhi; 10-21-2012 at 06:06 AM..
This User Gave Thanks to msabhi For This Post:
# 4  
Old 10-21-2012
Code:
for i in *[0-9].ext 
do 
file=$(echo "$i" | awk -F "" '{for(i=1;i<=NF;i++){if($i ~ /[A-z]/){s=""}else{if($i == "."){print s}else{s=s?s""$i:$i}}}}') 
file_name=$(echo "$i" | awk -v RPL="$file" '{sub(RPL,"",$0);print RPL,$0}') 
mv $i $file_name 
done

maybe your script works but my level of understandings don't allow me to tell you anything on it !

Last edited by mc2z674gj; 10-21-2012 at 11:43 AM..
# 5  
Old 10-21-2012
Is there any reason why all of that code is on a single line? Do you think that helps your 'level of understanding'?

For me, it's unreadable.

edit: you updated it. It's marginally more readable. Smilie

Last edited by Scott; 10-21-2012 at 12:02 PM.. Reason: ...
# 6  
Old 10-21-2012
Code:
ls *ext | perl -F"--" -alne '{if($#F==1){ 
if($F[1]=~/(\d+).ext/){ $p=$1;$F[0]=~s/^/$p /g;$F[1]=~s/$p//;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
else { ($F[0],$F[1])=($F[1],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[1]=~s/ *$/.ext/;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
}else{ ($F[0],$F[2])=($F[2],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[2]=~s/ *$/.ext/;;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
}' | sh

for moving numeric to the beginning, this script works perfectly !

???
i have a question can the script is capable to rename directories
# 7  
Old 10-21-2012
Quote:
Originally Posted by mc2z674gj
Code:
ls *ext | perl -F"--" -alne '{if($#F==1){ 
if($F[1]=~/(\d+).ext/){ $p=$1;$F[0]=~s/^/$p /g;$F[1]=~s/$p//;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
else { ($F[0],$F[1])=($F[1],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[1]=~s/ *$/.ext/;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
}else{ ($F[0],$F[2])=($F[2],$F[0]);$F[0]=~s/(.ext|^ *)//;$F[2]=~s/ *$/.ext/;;print "mv \"$_\" \"".join(" -- ",@F)."\"" ;} 
}' | sh

for moving numeric to the beginning, this script works perfectly !
I believe this script works for all your conditions..

???
i have a question can the script is capable to rename directories
Can you please give me some examples before i can answer you...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Manipulating files

Not sure if the question posted in another forums can be moved by me.So posting the link here. https://www.unix.com/unix-advanced-expert-users/221425-shell-script-manipulate-files.html#post302795379 Need your help here. (1 Reply)
Discussion started by: vedanta
1 Replies

3. Programming

Code imrovements on manipulating strings

I have written the code below and would be very grateful for any comments about it (how can I improve it, simplify it,...). #ifndef String_hh #define String_hh #include <stdio.h> #include <string.h> #include <stdlib.h> #include "Vect2.hh" #include "Vector.hh" #include... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

5. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

6. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

7. Windows & DOS: Issues & Discussions

Renaming files with strings from xml tags

Hello! I need to rename 400+ xml files. The name of the specific file is contained in a xml tag in the file itself. The batch file should rename all these files with strings found in xml tags. Every xml file has the following tags: <footnote><para>FILENAME</para></footnote> I have to get... (3 Replies)
Discussion started by: degoor
3 Replies

8. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

9. Solaris

Manipulating File

Help...please. I have a log that contains Warning Authentication Failed: User GHDT88998HS doesn't exit: The User GHDT88998HS could not be found Mar 22, 2008 5:22:22AM com.hometel.ttm.auth.userlogin. about maybe a thousand entries failed user acct message How can I grab just the username... (2 Replies)
Discussion started by: rivendell500
2 Replies

10. UNIX for Advanced & Expert Users

Manipulating two files

Hi Friends, I prefer to represent my problem with example. I have two files as below: file1.txt --------- abcd.....1234......XY abcd.....1235......XX abcd................. abcd...231236..1111YX abcd...241236..1112YY abcd...241237......YY abce.....1235......YY file2.txt ------- ... (4 Replies)
Discussion started by: rinku11
4 Replies
Login or Register to Ask a Question