1833790 Members
2537 Online
110063 Solutions
New Discussion

awk script

 
SOLVED
Go to solution
Duffs
Regular Advisor

awk script

Hi,

Iam having some difficulty with an awk statement. Within a certain dir I want to rename all WUK<8_digits>.dat files TAR<8_digits>.dat, duplicating the WUK files but replacing the TAR with WUK in the filename.

I have tried:

for i in `ll| awk '{ print $4 $15 }' WUK*`
do
cp WUK$i TAR$i
done

.....but no joy???

Rgds,
D.
9 REPLIES 9
TwoProc
Honored Contributor

Re: awk script

for i in WUK????????.dat
do
cp -p $i `echo $i | sed -e "s/WUK/TAR"`
done

We are the people our parents warned us about --Jimmy Buffett
Duffs
Regular Advisor

Re: awk script

Hi John,

Thanks for the speedy response! I am now getting the following output from the debug:

+ sed -e s/WUK/TAR
+ echo WUK????????.dat
sed: Function s/WUK/TAR cannot be parsed.
+ cp -p WUK????????.dat
Usage: cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file target_file
cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file ... target_direc
tory
cp [-f|-i] [-p] [-S] -R|-r [-e warn|force|ignore] source_directory ... t
arget_directory

Rgds,
D
TwoProc
Honored Contributor

Re: awk script

I've run across that before ... lemme play with it a bit over here...
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor
Solution

Re: awk script

Well - I'm used to using -e and putting quotes (either for actual reasons that it works - or just a brain fart - I dunno) ... anyway this works over here in a test set I made that matches your file names...


for i in WUK????????.dat
do
cp -p $i `echo $i | sed s/WUK/TAR`
done

Run ...

testhost:/tmp/junk/ for i in WUK????????.dat; do cp -p $i `echo $i | sed s/WUK/TAR/`; done
testhost:/tmp/junk/ ls -al
total 0
-rw-r--r-- 1 root sys 0 Jul 12 11:12 TAR12335678.dat
-rw-r--r-- 1 root sys 0 Jul 12 11:11 TAR12345678.dat
-rw-r--r-- 1 root sys 0 Jul 12 11:12 WUK12335678.dat
-rw-r--r-- 1 root sys 0 Jul 12 11:11 WUK12345678.dat
We are the people our parents warned us about --Jimmy Buffett
John Poff
Honored Contributor

Re: awk script

Hi,

You can do it without using awk. Try this:

for i WUK*
do
n=${i##WUK}
cp $i TAR${n}
done



JP
TwoProc
Honored Contributor

Re: awk script

Cool JP, I knew I had read some time back that I could do something like this in the shell itself, but couldn't recall where. I've been mentally shaming myself each time I used awk to do this (when you have a hammer everything looks like a nail) - remembering that I'd read somewhere how to do it directly in the shell.
We are the people our parents warned us about --Jimmy Buffett
Duffs
Regular Advisor

Re: awk script

Thanks a million lads!
Kind Rgds,
D
TwoProc
Honored Contributor

Re: awk script

(No more points) ... but going further with the information provided by JP (just for fun):

This is pretty small and does the job.

for i in WUK*; do cp $i ${i/#WUK/TAR}; done
We are the people our parents warned us about --Jimmy Buffett
Sandman!
Honored Contributor

Re: awk script

Dermot,

You can use this one-liner awk program.

# ls -1 WUK[0-9]*.dat | awk -F"WUK" '{system("cp "$0" TAR"$2)}'

cheers!