awk help

Hi All,

I was looking for a quick AWK/SED one liner that will return me the following:

  1. Search for a pattern.
  2. After fourth occurrence of the pattern in the file, the value is on the 7th line and 5th field.

Appreciate any help in advance.

Thanks.

Please be aware that the better your problem description and the more care is deployed to create it, the better the answers you'll get. Sample input and desired output data definitely belong here.
With 184 posts, you should have at least a vague idea about how to approach it. So - any attempts / ideas / thoughts from your side?

Hi RudiC,

Thanks for the feedback. This is the sample input and output.

INPUT:

lkshfoishfsaf aaa oiuagfakjfoia lahofihapofd

jaodadoh poaihdaihdpaoihd iohhadaihd
adkhlksahdlsakd aaa asldhaslkhdsad

akdpsad adsasalkhdsa aslkdhsadksa
aaa lkafhdlsakhfdsald lkahdlkahdsa

alsjdsa;ld adlksakhdsalhd
;ljsafdjsadkjapodja aaa lkahdlksahdpsahdsa

adwahd;lksahdpsahdsadsads
akdhlkAHDahdAD
alkdhLHDS;ldsA

ALDahdlkAHDalkdh alkdhlad
adlkhadlksahd lakhdlkahdsald

adlahdlkhadlahdwadh salchaldwadjpa
asldkhsaldhsa;ld asdlkdlhsad iuadasoidsad asdoad VALUE hjadsadhosad asdlkhsadkhsad

alkfdhsalkdhsadhsadhsa aaa alkdhsaldhsalhdsad

OUTPUT:

VALUE

I was trying something like this but gave me an error:

awk '/aaa/ {count++} if (count == 5) NR == 7 {print $5} END {print count}' test.txt

Rearranging/correcting your approach as the spec doesn't match the data, try:

awk '/aaa/ {if (++count == 4) PRL = NR + 10}  NR == PRL {print $5}' file
VALUE
2 Likes

If the pattern counts if it occurs more than once on a line, try:

awk 'NR==n{print $5} !n{c+=gsub(/aaa/,"%"); if(c>=4)n=NR+10}' file

If the pattern counts if it occurs more than once on a line and needs to be a standalone word, try:

awk 'NR==n{print $5} !n{for(i=1; i<=NF; i++) if($i=="aaa") c++; if(c>=4)n=NR+10}' file

If the pattern counts once per line and needs to be a standalone word, try:

awk 'NR==n{print $5} !n{for(i=1; i<=NF; i++) if($i=="aaa") {c++; break}; if(c>=4)n=NR+10}' file

If the pattern counts once per line and may be part of a word, try:

awk 'NR==n{print $5} /aaa/ && ++c>=4{n=NR+10}' file
1 Like

Thanks! That worked.

What if I want the same for multiple occurrences in the file? Can I put it in a loop?

The pattern to search is aaa1 , aaa2 , aaa3 , etc.

Input file:

lkshfoishfsaf aaa1 oiuagfakjfoia lahofihapofd

jaodadoh poaihdaihdpaoihd iohhadaihd
adkhlksahdlsakd aaa1 asldhaslkhdsad

akdpsad adsasalkhdsa aslkdhsadksa
aaa1 lkafhdlsakhfdsald lkahdlkahdsa

alsjdsa;ld adlksakhdsalhd
;ljsafdjsadkjapodja aaa1 lkahdlksahdpsahdsa

adwahd;lksahdpsahdsadsads
akdhlkAHDahdAD
alkdhLHDS;ldsA

ALDahdlkAHDalkdh alkdhlad
adlkhadlksahd lakhdlkahdsald

adlahdlkhadlahdwadh salchaldwadjpa
asldkhsaldhsa;ld asdlkdlhsad iuadasoidsad asdoad VALUE1 hjadsadhosad asdlkhsadkhsad

alkfdhsalkdhsadhsadhsa aaa1 alkdhsaldhsalhdsad


lkshfoishfsaf aaa2 oiuagfakjfoia lahofihapofd

jaodadoh poaihdaihdpaoihd iohhadaihd
adkhlksahdlsakd aaa2 asldhaslkhdsad

akdpsad adsasalkhdsa aslkdhsadksa
aaa1 lkafhdlsakhfdsald lkahdlkahdsa

alsjdsa;ld adlksakhdsalhd
;ljsafdjsadkjapodja aaa2 lkahdlksahdpsahdsa

adwahd;lksahdpsahdsadsads
akdhlkAHDahdAD
alkdhLHDS;ldsA

ALDahdlkAHDalkdh alkdhlad
adlkhadlksahd lakhdlkahdsald

adlahdlkhadlahdwadh salchaldwadjpa
asldkhsaldhsa;ld asdlkdlhsad iuadasoidsad asdoad VALUE2 hjadsadhosad asdlkhsadkhsad

alkfdhsalkdhsadhsadhsa aaa2 alkdhsaldhsalhdsad

OUTPUT:

aaa1 VALUE1
aaa2 VALUE2
aaa3 VALUE3
...
...

With what you learned in this thread - how would you go about it?

1 Like