I need to achieve the following.....I seached the forum but could not find it...
This is I have in a file...
"CH","TIA","10/27/2006",000590
I need the date in the third field to be attached to fileas 20061027_test.txt
How do I do it.
I need to achieve the following.....I seached the forum but could not find it...
This is I have in a file...
"CH","TIA","10/27/2006",000590
I need the date in the third field to be attached to fileas 20061027_test.txt
How do I do it.
$ cat file
"CH","TIA","10/27/2006",000590
$ sed 's;.*"\([0-9]*\)/\([0-9]*\)/\([0-9]*\).*;\3\1\2;' file
20061027
dt=$( sed 's;.*"\([0-9]*\)/\([0-9]*\)/\([0-9]*\).*;\3\1\2;' file )
mv filenm ${dt}"_"filename
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{print>(dt[3] dt[2] dt[1] "_" FILENAME)}' FS="," inputfile
Use nawk on Solaris.
if you have Python, here's an alternative
import os
myfile = "filename"
for line in open("file"):
dt = line.split(",")[2][1:-1].split("/")
newname = "%s%s%s_test.txt" %(dt[2],dt[0],dt[1])
os.rename(myfile,newname) #rename file to new filename with date attachment
dt_str=$(awk -F"," '{gsub(/"/,"",$3); split($3,arr,"/"); print arr[3] arr[1] arr[2];}' filename)
mv filename ${dt_str}"_"filename
perl -e 'while(<>) { chomp; s/\"//g; @a = split(/,/); @now = split(/\//, $a[2]); system("mv filename filename_$now[2]$now[0]$now[1]"); }' filename
With gawk:
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{print "mv", FILENAME, (dt[3] dt[2] dt[1] "_" FILENAME)|"sh";nextfile}' FS="," inputfile
Otherwise:
awk 'FNR==1{gsub(/\"/,"",$3);split($3,dt,"/")}
{f[FILENAME]=(dt[3] dt[2] dt[1] "_" FILENAME)}END{for(i in f)print "mv",i,f|"sh"}' FS="," inputfile