user236520
user236520

Reputation:

How can I extract 2 pieces of information output from command though pipes in a bash script?

I have a command that produces the following output:

{i} count
{0} 167

real    0m1.370s
user    0m0.008s
sys     0m0.000s

I would like to pipe this output into something that will extract items on line 2 and line 4, and output them in a single line as follows:

167 0m1.370s

I have often used sed and awk to get a single value, but can i gerrymander these stream tools to process a multiline output?

Upvotes: 1

Views: 97

Answers (3)

anubhava
anubhava

Reputation: 785156

You can use pipe your output to this awk:

awk '/\{0\}/{p=$2} /real/{print p, $2; exit}'

Upvotes: 4

Ed Morton
Ed Morton

Reputation: 203532

With GNU awk for multi-char RS:

$ awk -vRS='^$' '{print $4, $6}' file
167 0m1.370s

Upvotes: 1

JNevill
JNevill

Reputation: 50034

If it's always the second element of the second and fourth line you can pipe to the following:

awk 'NR==2 {p=$2} NR==4 {print p, $2; exit}'

Upvotes: 1

Related Questions