Samy
Samy

Reputation: 49

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 * 
do
    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

Upvotes: 1

Views: 113

Answers (3)

dawg
dawg

Reputation: 103764

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.


Explanation:

gawk '/\<n=/ && /\<perf=/   # match n= and perf= in a line
   #    ^         ^       only if the n and perf are not the ending of another word

  {match($0, /\<n=\s*([[:digit:]]+)/, a);  # extract n= number
   match($0, /\<perf=\s*([[:digit:]]+)/, b); # extract perf= number
   printf "%s %s\n", a[1], b[1]}'         # print those two numbers

Since it is written as two independent matches, the n= and perf= can be in any order on the line.

Upvotes: 0

nopasara
nopasara

Reputation: 536

How about:

awk -F',' '/^.*n=.*perf=.*$/{split($4,a,"= ");split($6,b,"= ");print a[2]" "b[2];}' file*

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246774

perhaps

perl -nE '/\bn= (\d+).*\bperf= ([\d.]+)/ and say "$1 $2"' * > output

Upvotes: 1

Related Questions