Search files and run a script on every result - Cont:

VNA

I would like to know how to search certain pattern of files (GunZip Files) in all Sub Directories ( Month wise / Date wise - Sub Directories created). And then, execute a script on the found files. Also need to populate FILENAME along with output for tracking purpose and further analysis on that particular files.

Step1: For example: currently searching files on this pattern TT_DETAIL*.gz.

find /cygdrive/c/Test/  -name TT_DETAIL*.gz

output#1:

/cygdrive/c/Test/Feb2014/TT_DETAIL_20141115.csv.gz
/cygdrive/c/Test/Jan2014/TT_DETAIL_20141110.csv.gz
/cygdrive/c//Test/Mar2014/TT_DETAIL_20141120.csv.gz

Step2:

zcat TT_DETAIL*.gz | awk 'BEGIN { FS=OFS=","} { if ($11=="10") print $2,$3,$6,$10,$11,$17}' >Op_TT_Detail.txt

cat Op_TT_Detail.txt

ZZZ,AAA,ECH,1,10,XXX
ZZZ,BBB,ECH,1,10,XXX
ZZZ,CCC,ECH,1,10,XXX
ZZZ,DDD,ECH,1,10,XXX

Thanks fedorqui for below script is working fine without FILENAME.

while IFS= read -r file
do
   awk 'BEGIN { FS=OFS=","} { if ($11=="10") print $2,$3,$6,$10,$11,$17}' <(zcat "$file") >>Op_TT_Detail.txt
done < <(find /cygdrive/c/Test/  -name TT_DETAIL*.gz)

Have tried below command to populate FILENAME along with output for tracking purpose :

while IFS= read -r file
do
   awk 'BEGIN { FS=OFS=","} { if ($11=="10") print $2,$3,$6,$10,$11,$17,FILENAME}' <(zcat "$file") >>Op_TT_Detail.txt
done < <(find /cygdrive/c/Test/  -name TT_DETAIL*.gz)

Desired Output:

 ZZZ,AAA,ECH,1,10,XXX,/cygdrive/c/Test/Feb2014/TT_DETAIL_20141115.csv.gz
 ZZZ,BBB,ECH,1,10,XXX,/cygdrive/c/Test/Feb2014/TT_DETAIL_20141115.csv.gz
 ZZZ,CCC,ECH,1,10,XXX,/cygdrive/c//Test/Mar2014/TT_DETAIL_20141120.csv.gz 
 ZZZ,DDD,ECH,1,10,XXX,/cygdrive/c//Test/Mar2014/TT_DETAIL_20141120.csv.gz 

Since FILENAME is not working for *.gz files , should I write" find /cygdrive/c/Test/ -name TT_DETAIL*.gz " into another output file then call that output file into script , I don't have a write access for source files located server.

Looking for your suggestions !!!

fedorqui 'SO stop harming'

Nice to see you are using the snippet I wrote in the previous question!

I would use this:

while IFS= read -r file
do
   awk -v file="$file" 'BEGIN { FS=OFS=","} \
                        { if ($11=="10") print $2,$3,$6,$10,$11,$17, file}' \
        <(zcat "$file") >>Op_TT_Detail.txt
done < <(find /cygdrive/c/Test/  -name TT_DETAIL*.gz)

That is, with -v file="$file" you give the file name as a variable to awk. And then you use it in your print command.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related