file name transformation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file name transformation
# 8  
Old 05-22-2008
Use a loop, for instance:

Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/\1\3-\2\4/' "$file" > "$file"_new
  mv "$file"_new "$file"
done

Regards
# 9  
Old 05-23-2008
Hi guys the above script looks perfect (even to my untrained eye)

and the previous one

$ echo "daf_1961-04_act.txt"|sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/\1\3-\2\4/'
$ daf_04-1961_act.txt

has shown me that the sed script works

however when I run it in a loop


Quote:
Originally Posted by Franklin52
Use a loop, for instance:

Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/\1\3-\2\4/' "$file" > "$file"_new
  mv "$file"_new "$file"
done

Regards
I see the new files being created (They are quire big so they take a while)

However the file names produced as a result of the loop aren't changed (they come out exactly as they went in). It's really making me scratch my head. I think the problem's 99% sorted but somethings' wrong.

Thanks
# 10  
Old 05-23-2008
remove the move (mv) command and you will get the desired result.
# 11  
Old 05-23-2008
I'm afraid that hasn't had the desired effect rana_d

But thanks for the suggestion

vrms
# 12  
Old 05-23-2008
I'm sorry for the misunderstanding, this should give the command to move the files:

Code:
echo "daf_1961-04_act.txt"|sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/'

Try it in a loop to see if the you get the desired output to mv the selected files:

Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/' "$file"
done

If the output is correct you can pipe the output of the sed command to sh to mv the files in the loop as follow:

Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/' "$file" | sh
done

Regards
# 13  
Old 05-23-2008
Quote:
Originally Posted by Franklin52
Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/' "$file"
done

When I run it sends the whole content of the files in the directory to the screen



[QUOTE]If the output is correct you can pipe the output of the sed command to sh to mv the files in the loop as follow:

Code:
for file in *.txt
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/' "$file" | sh
done

Regards[QUOTE]


When I run it, this comes up and the filenames are unchanged

sh: llllll: not found
sh: l: not found
sh: lllllllllllllllllll: not found
sh: rrrrrrrrrrrrrrrr: not found
sh: rrrrrrrrrrrrrrrrr: not found
sh: rrrrrrrrrrrrrrrrr: not found
sh: rrrrrrrrrrrrrr: not found


Where llllllllllllllll and rrrrrrrrrrrrrrrrrr etc are the contents of some dummy files I'm using to verify the scripts

So I'm afraid something is not quite right again

Thanks

vrms
# 14  
Old 05-23-2008
I was assuming that you have only .txt files with names in the same format.
To select filenames with a format like "daf_1961-04_act.txt" you can do something like:

Code:
for file in `ls | grep ".*_....-.._.*"`
do
  sed 's/\(.*_\)\(.*\)-\(.*\)\(_.*\)/mv & \1\3-\2\4/' "$file"
done

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data transformation

I do have an input text file of the following format with 1000's of lines input file: 3386(11:11,Ani:0,Bri:1,ch:1,Jwe:0,Jor:0,LP:0,Lo:0,NS:1,al:1,bo:0,boy:0,bru:0,sh:0,cor:1,dum:0,ery:0,mac:0,mic:0)... (3 Replies)
Discussion started by: Kanja
3 Replies

2. Shell Programming and Scripting

Row to Column transformation

Hello Experts, I need to transform rows into column using awk. I tried few things but failed to obtain desired output, as I'm fairly new to awk. i/p file 100, READ, 12 100, WRITE, 8 100, SEEK, 1 142, READ, 2 142, WRITE, 34 142, SEEK, 3 O/p Needed PROC_ID 100 142 READ 12 ... (2 Replies)
Discussion started by: sybadm
2 Replies

3. UNIX for Dummies Questions & Answers

file transformation using fixed width file

Hi Gurus! I need to make some file transformations. Please help. This is my input file. It has four columns with fixed width. 1 aaa bbbb cccc 2 eee dddd jjjj 3 fff gggg jjjj 4 hhh iiii cccc 5 kkk llll cccc 6 mmm nnnn oooo 7 ppp qqqq xxxx 8 rrr ... (1 Reply)
Discussion started by: kokoro
1 Replies

4. Shell Programming and Scripting

Clipboard transformation scripting

Hello all, I've done a bit of clipboard transformation scripting using xclip before, piping contents with " xclip -o -selection clipboard " to grep, sed, awk, then back into the clipboard with " xclip -i -selection clipboard " ... but I am not a fantastically skilled user of either of the three... (4 Replies)
Discussion started by: la2ar0
4 Replies

5. Shell Programming and Scripting

XML file transformation

Hi all, I have to transform a XML file like this: <?xml version="1.0"?> <vocabulary> <voc_id>102</voc_id> <name>Vocabulary Name</name> <description>Voc description</description> <relations>3</relations> <hierarchy>5</hierarchy> <word> <word_id>1</word_id> ... (1 Reply)
Discussion started by: aLittleBeat
1 Replies

6. UNIX for Advanced & Expert Users

Need help in xslt transformation

Hi I have one input xml file <param name="EXTR_COL" valueDesc="AUTHD_RFLL" value="rx.AUTHD_RFLL" /> There is a mapping parameters in Database. if EXTR_COL is present in input XML then it is mapped to fieldlist. so the o/p XML looks like <fieldlist> <datasource... (1 Reply)
Discussion started by: srinu19
1 Replies

7. Shell Programming and Scripting

xslt transformation through Unix

Hi .. I have one input XML and I want to convert into another XML using parameter mapping through Database through Unix shell script. But I dont have idea how to do that. And how can I create xsl sheet if mapping is through database tables. Please help me on this. (1 Reply)
Discussion started by: srinu19
1 Replies

8. Shell Programming and Scripting

File transformation - what is most efficient method

I've done quite a bit of searching on this but cannot seem to find exactly what I'm looking for. Say I have a | delimited input file with 6 columns and I need to change the value of a few columns and create an output file. With my limited knowledge I can do this with many lines of code but want... (5 Replies)
Discussion started by: 1superdork
5 Replies

9. UNIX for Dummies Questions & Answers

Transformation capital letter

:confused: Hye everybody i would like to know if exist a internet site where i can founs some interesting shell script very usefull I need to transform hundreds names of files escribed in CAPITAL letter in minuscule letter do oyu know a mean o do that that thanks to a script or a shell... (1 Reply)
Discussion started by: Dark Angel
1 Replies
Login or Register to Ask a Question