How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts,
I am trying to get the output from a matching pattern but unable to construct the awk command:

file :

aa bb cc 11
dd aa cc 33
cc 22 45 68
aa 33 44 44
dd aa cc 37
aa 33 44 67

I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and if $1 matches to "dd" of start of that line then print $4 of that line).

I am trying with this:

 awk '/^aa/{A=$4};/^dd/{D=$4}END{print A,D}' file 

But not working!, it is only printing the last finding seems to be.

The out put needs to be:

aa 11
dd 33
aa 44
dd 37
aa 67

Thanks.

From what I see is that you are trying to print $1 and $4.

I don't see any complexions here on your output!

Hey,

Try something like this,

awk '/^aa|^dd/{print $1,$4;}' file

Cheers,
Ranga:)

1 Like
awk '$1=="aa" || $1=="dd" {print $1,$4}'
aa 11
dd 33
aa 44
dd 37
aa 67
1 Like