find pattern matches in consecutive lines in certain fields-awk
I have a text file with many thousands of lines, a small sample of which looks like this:
InputFile:
What I would like to do is that if a given line meets the conditions $16=="0" && $22=="-1" and the immediately following line has $22=="503" && $4=="7" && $16=="2" then print every set of these two consecutive lines.
Desired Output:
Thus far I have tried various revisions of the following awk code which has gotten me fairly close:
Nevertheless, I continue to not be able to figure out how to get this to work. I would very much appreciate any help to get this one-liner to work as desired. Thanks!
Last edited by jvoot; 08-09-2018 at 08:23 PM..
Reason: Left off one condition to arrive at desired output.
Thank you so much vgersh99. Indeed, in the verbal parameters of my desired output I left out one condition (viz., $16=="2"). I have since fixed my original post. Thanks for pointing that out to me and the help with the code.
A slight adjustment to the code you offered reached the desired output as indicated in the original post.
I always prefer to have a state variable and a store variable.
The { met=0 } clears the state, in order to only continue the search in the immediately following line.
The order 2. condition then 1. condition saves a next.
This User Gave Thanks to MadeInGermany For This Post:
In this case @MadeInGermany, I would not want any output to be generated given your proposed input file.
Maybe it would be helpful if I gave some more sample data from my input.
InputSample
Desired Output
Thus, when there is a line that meets the conditions:
Check the immediately following line to see if it has the conditions:
If both of these are met, then print both lines to output; else do nothing. Ideally, I would like to be able to use the code help I receive here as a kind of template to vary the conditions on the various fields to extract a wide range of data patterns. I attempted the code that you offered and while I have not checked its accuracy in detail due to the size of the output, it seems that at a first glance it worked perfectly.
Dear gurus,
Please help this beginner to write and understand the required script. I am looking for useing awk for sed.
I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings.
1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Hi,
I would like to know how to remove lines which has the same pattern as the next line through sed/awk.
Stream 39 (wan stream 7)
Stream 40 (wan stream 8)
WINQ Counter 115955 1 1613
(BYTE) 11204787 163 ... (2 Replies)
Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills":
I need to find a pattern /VEL/ in an input file that looks like this:
1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736
... (3 Replies)
I know how to search for a pattern/regular expression in many files that I have in a directory. For example, by doing this:
grep -Ril "News/U.S." .
I can find which files contain the pattern "News/U.S." in a directory.
I am unable to accomplish about how to extend this code so that it can... (1 Reply)
I have following pattern in a file:
00:01:38 UTC
abcd
00:01:48 UTC
00:01:58 UTC
efgh
00:02:08 UTC
00:02:18 UTC
and I need to change something like the following
00:01:38 UTC
abcd
00:01:48 UTC
XXXX
00:01:58 UTC
efgh
00:02:08 UTC
XXXX (6 Replies)
Hi ,
I'm looking for a way to merge two lines only for a given pattern / condition.
Input :
abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre *
fdsgfds/dsgf/sdfdsfasdd +
-4.30 0.62 -49.5 45 sdfdsf cvbbv *
sdfds/retret/asdsaddsa +
... (1 Reply)
I have a file like this. Pls help me to solve this .
(I should look for only Message : 111 and need to print the start time to end time
Need to ignore other type of messages. Ex: if first message is 111 and second message is 000 or anything else then ignore the 2nd one and print start time of the... (1 Reply)
I have one comma separated file (a.txt) with two or more records all matching except for the last column.
I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily?
I've searched the forum but most talked... (6 Replies)
Howdy.
I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited?
Example:
You are... (3 Replies)