Mind5 Mind5 - 4 months ago 20
Bash Question

Checking CSV By awk Condition By Condition

i'v 2 Conditions with 2 different results to make in new CSV file.

i used awk reading all the csv file and checking if the 1st condition is work give us result and copy the line to new CSV.
another condition i'v used with different result.
the line that have the 2nd condition will also be copied to the same CSV.

My code:

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



zcat "$FileName" | awk -F'\t' '(($4 >=300) && ($4 <=399)) {Str="Redirect: " $3;print Str > "New.csv"}'


the result is that i'v new csv with just the line with the 2nd condition.

Thanks

Answer

use >> to append contents to a file

> redirection will overwrite if file already exists

so, the second command would be:

zcat "$FileName" | awk -F'\t' '(($4 >=300) && ($4 <=399)) {Str="Redirect:  " $3;print Str >> "New.csv"}'

Further Reading: Redirecting Output of print and printf