awk and execute command ???

If I have a flat file date.txt separate by a tab

date1 date2 count
20060226 20060225 2

20060227 20060226 2

20060228 20060227 5

20060314 20060228 2

20060315 20060314 1

If $3 (count) is 5 then execute the script testit.ksh with $1=20060228 , using script testit.ksh 20060228
and if $3 is 5 then $2=20060227 and move files *.20060227.* to /DAT2/

cat date.txt |
awk '{
if ($3 == 5) {
./testit.ksh $1 ????
mv *.$2.* /DAT2/

}
}'

This is a stupid code :frowning: but I just want to learn what you do with awk and command .
Thanks for teaching me.

wouldn't it be easier to do it all in ksh?

not tested.......

#!/bin/ksh

while read d1 d2 cnt
do
    (( cnt = 5 )) && (( d1 == 20060228 )) && testit.ksh "${d1}"
    (( cnt = 5 )) && (( d2 == 20060227 )) && cp *.${d2}.* /DAT2/
done < myFile.txt

I use awk because I want to findout $3 is 5, then take $1 and $2, I really dont know what will be $1 and $2 .Just use it to move or execute another script :slight_smile:

ok. what's your point?
have looked at/tried the posted suggestion?

Hi vgersh99,
Because this myFile.txt have along list and I am sure it have cnt is = 5, then take that $1 is d1 at $2 is d2. So I update your code like this but it copy every thing to DATA2 directory and somehow it can not execute the testit.ksh. Any idea ?

#!/bin/ksh

while read d1 d2 cnt
do
(( cnt = 5 )) && testit.ksh "${d1}"
(( cnt = 5 )) && cp *.${d2}.* /DAT2/
done < myFile.txt

could you post a sample 'myFile.txt', pls

also is your 'testit.ksh' has the 'execute' bit turned on?

Hi,

Here is what I did and it work

cat date.txt |
awk '{
if ($3 == 5) {
system("testit.ksh " $1);
system("mv " $2 ". " "/DATA2/ ");

}
}'

congrats!

  1. UUOC
  2. tried running it on large file? You are not using 'close' for every 'system' call - you'll run out of the fd's
  3. why are you using awk for this task?

Oh I am a newbie so what make it work then I do it . I tried your code but some how I keep geting all of the file move to DATA2 instead of the files have 5 counts. Sorry for my English..

From this forum I have learned alot from you and the other.
Good site and thanks for sharing vgersh99

could you post a sample of your date.txt file, pls
Also it might be a good idea to start posting using vB codes so that we get properly formated samples.

Oh here is the file date.txt
20060122 20060121 1
20060214 20060122 1
20060215 20060214 1
20060216 20060215 1
20060217 20060216 1
20060220 20060217 1
20060221 20060220 3
20060222 20060221 2
20060223 20060222 1
20060224 20060223 1
20060225 20060224 1
20060226 20060225 2
20060227 20060226 2
20060228 20060227 5
20060314 20060228 2
20060315 20060314 1
20060316 20060315 1
20060317 20060316 1
20060318 20060317 1
20060319 20060318 1
20060320 20060319 2
20060321 20060320 1

and the code

cat date.txt |
awk '{
if ($3 == 5) {
system("testit.ksh " $1);

    system\("mv *" $2 ".dat /DATA2/ "\);

}
}'

I want to run script for

  1. $testit.ksh 20060228 because it have all 5 files like A-20060228.dat, B-20060228.dat, C-20060228.dat, D-20060228.dat, E-20060228.dat

  2. I want to move file with the name *-20060227.dat to /DATA2/

given this script:

#!/bin/ksh


file='date.txt'

while read d1 d2 cnt
do
    (( cnt = 5 )) && (( d1 == 20060228 )) && echo testit.ksh "${d1}"
    (( cnt = 5 )) && (( d2 == 20060227 )) && echo cp *.${d2}.* /DAT2/
done < "${file}"

with 'date.txt' file being as you posted last......
I get the following 'debug' output:

testit.ksh 20060228
cp *.20060227.* /DAT2/

Is that something you're looking for?

Yes, I use this script to clean up my directory and archive files :slight_smile: . At first I get all the date of the files and sort it then uniq it to a date.txt file, then I process all of the files before 5 count, to move it out of directory, they suppose to have all 5. not 1 and 2 like I test :slight_smile:

hmm.....
I'm a bit confused by the above statement, but that's ok - would not be the first time.

I guess as long as you're happy with whatever solution you're using....