Samy Samy - 1 year ago 71
Bash Question

Shell script: skip column if not found

I have too many files that contain one line like this:

Name: transa= N, transb= N, m= 40600, n= 40600, k= 40600, perf= 1635.266 gf

I need to grab n and perf from all of them. The think is some files are empty or have errors. Here is what i have now:

for file in *
awk -F "," '{print $(NF-2), $NF}' file1* 2>&1|tee "file1.csv"
awk -F "," '{print $(NF-2), $NF}' file2* 2>&1|tee 2>&1|tee "file2.csv"
done 2>&1 | tee "everything.csv"

I get errors as soon as this loops over an empty or error file. Please suggest how to check if (NF-2) doesn't exist.

Also, now I get as resuls:

n= 1000 perf= 1810.386 gf
n= 10000 perf= 4996.192 gf
n= 13600 perf= 4870.097 gf
n= 1600 perf= 2661.830 gf

How do I get just:

1000 1810.386
10000 4996.192

Thank you

Answer Source

You can do:

gawk '/\<n=/ && /\<perf=/ {match($0, /\<n=\s*([[:digit:]]+)/, a); match($0, /\<perf=\s*([[:digit:]]+)/, b); printf "%s %s\n", a[1], b[1]}' * > output

no need for the bash loop.

Note, this is gawk specific because of \< for the word boundary.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download