Search code examples
linuxshellawkzcat

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've 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.

Thanks


Solution

  • 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"}'
    

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

    Update:

    As pointed out by tripleee, the awk command should be:

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