awk to find pattern in file

Hi
I am using awk to find the pattern from the file, but it is not displaying anything. Probably I am missing some syntax in expression.

FILE

xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

I am using

CODE=BY
cat $LOCK_FILE | awk '/Merge.exchMon.$CODE.qStart/ && /Merge.exchMon.$CODE.qStop/ && /Merge.exchMon.$CODE.qTime/ && /DelayTracker.$CODE.tStart/ && /DelayTracker.$CODE.tStop1/'

Because qTime is in the first place, but I want to display qTime at the 3rd place so I tried above awk and tried searching pattern with && at 3rd place.

something along these lines?

CODE=BY
awk -v code="${CODE}"  -F. '$3==code' myFile
1 Like

Sorry, I forgot to mention
I want the output like below after awk

From

xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

To

xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

hm.... this was a confusing explanation to begin with.....

awk -v code="${CODE}"  -F'[. ]' '$4==code && $5=="qTime"{qt=$0;next}$5=="qStop" && qt{print qt;qt==""}1' myFile
1 Like

Wouldn't a simple sort help?

1 Like

Thanks vgersh99
But the awk is listing out entire file.
I wanted to find the pattern based on the CODE passed as an argument
that is, want the output as

xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read

---------- Post updated at 09:24 AM ---------- Previous update was at 09:22 AM ----------

Thanks RudiC
the sort always using the dictionary sort and showing the below output

xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read

Like so:

grep -F ".BY." file | sort -t. -k2,2r -k4,4 -k5,5
xxxx.Merge.exchMon.BY.qStart         09:55               read
xxxx.Merge.exchMon.BY.qStop          15:30               read
xxxx.Merge.exchMon.BY.qTime          120                 read
xxxx.DelayTracker.BY.tStart       09:55                   read
xxxx.DelayTracker.BY.tStop        15:30                   read
2 Likes
awk -v code="${CODE}"  -F'[. ]' '$4!=code{next} $5=="qTime"{qt=$0;next}$5=="qStop" && qt{print qt;qt==""}1' myFile

but RudiC's suggestion is more elegant - not sure if it'd work if there were multiple "blocks" in the same file :wink:

Thanks RudiC
that worked as expected :smiley:

Can you please also explain a bit what does this sort means (sort -t. -k2,2r -k4,4 -k5,5)

man pages frequently help.

man sort :

so

-t.	use dot as field separator
-k2,2r	first sort key: 2. field, reverse sort
-k4,4	next key: fourth field

etc.