Alex
Alex

Reputation: 47

awk multiple row and printing results

I would like to print some specific parts of a results with awk, after multiple pattern selection.

What I have is (filetest):

A    : 1
B    : 2

I expect to have:

1 - B : 2

So, only the result of the first row, then the whole second row. The dash was added by me.

I have this:

awk -F': ' '$1 ~ /A|B/ { printf "%s", $2 "-" }' filetest

Result:

1 -2 -

And I cannot get the full second row, without failing in showing just the result of the first one

awk -F': ' '$1 ~ /A|B/ { printf "%s", $2 "$1" }' filetest

Result:

1 -    A    2 -    B 

Is there any way to print in the same line, exactly the column/row that I need with awk? In my case R1C2 - R2C1: R2C2?

Thanks!

Upvotes: 0

Views: 715

Answers (5)

user7712945
user7712945

Reputation:

tried on gnu awk

awk -F':' 'NR==1{s=$2;next}{FS="";s=s" - "$0;print s}' filetest

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203512

I'd probably go with @jas's answer as it's clear, simple, and not coupled to your data values but just to show an alternative approach:

$ awk '{printf "%s", (NR%2 ? $3 " - " : $0 ORS)}' file
1 - B    : 2

Upvotes: 0

JBone
JBone

Reputation: 1794

You can try this

awk -F:  'NR%2==1{a=$2; } NR%2==0{print a " - " $0}' file

output

  1 - B : 2

Upvotes: 0

accdias
accdias

Reputation: 5372

This will do what you are expecting:

awk -F: '/^A/{printf "%s -", $2}/^B/{print}' filetest

Upvotes: 1

jas
jas

Reputation: 10865

$ awk -F: 'NR%2 {printf "%s - ", $2; next}1' filetest
1 - B : 2

Upvotes: 1

Related Questions