Mind5 Mind5 - 4 months ago 7
Linux Question

Shell code- Reading a CSV file by loop and making another one

I have to do Shell code that read CSV file line by line, checking every line in some columns values and making another new csv file according to the conditions.

i'v started the code like this:

FileName=$1
while read line
do

zcat FileName awk -F'\t' '$3 ~/.jar/ || $3 ~/.msi/ || $3 ~/.dll/ || $3 ~/.cab/ || $3 ~/.exe/ || $3 ~/.rar/ || $3 ~/.mar/ || $3 ~/.tgz/ || $3 ~/.tar/ || $3 ~/.gz/ || $3 ~/.upd/ || $3 ~/.bin/ || $3 ~/.zip/ || $3 ~/.pogo/ || $3 ~/.dcr/ || $3 ~/.qgi/ || $3 ~/.deb/ || $3 ~/.ipa/ || $3 ~/symantec/ || $3 ~/windowsupdate/ || $3 ~/kaspersky/ || $3 ~/adobe.com/ && $42 ~/7/ && $15 ~/androiddownloadmanager/ && $15 ~/.apk/ && $15 ~/.pak/ && $15 ~/.vpx/ {Str="SW Download: " $3;Str>> New.csv} '

echo "$line" >> new2.csv

done < FileName


note: the file name is .gz .

(1)i got the error : "syntax error near unexpected token `done'" .

(2)i want to check if the new csv will be done and contain lines.

Thank's

Answer

You don't need the shell loop. Just pipe you zcat output to awk:

FileName=$1

zcat "$FileName" | awk -F'\t' '($3 ~/\.jar/ || /\.msi/ || /\.dll/ || /\.cab/ || /\.exe/ || /\.rar/ || /\.mar/ || /\.tar/ || /\.gz/ || /\.upd/ || /\.bin/ || /\.zip/ || /\.pogo/ || /\.dcr/ || /\.qgi/ || /\.deb/ || /\.ipa/ || /symantec/ || /windowsupdate/ || /\.kaspersky/ || /adobe\.com/) && $42 ~/7/ && ($15 ~/androiddownloadmanager/ && /\.apk/ && /\.pak/ && /\.vpx/) {Str="SW Download: " $3;print Str > "New.csv"}'

You can also shorten your awk command by not repeating the field for each pattern and by grouping field matchings with brackets as above.

And the right syntax to use a variable is $FileName, not FileName.

Comments