Compare between current and next line and print

Dear All

I want below to compare two Consecutive line(i.e. current and next line). Based in that i need OP. Below is the IP file in that in i find "M" and if in next line i find "" then i need both line in single line. If i dont find "" in next line then i need to put commend "DOWN" .

I am using below script but its not print all line. Its ommite some line. Kindly suggest changes.

Script i am using:

{
if ($3 =="M") {
a=$0;
	{
		getline;
		b=$2;
	if ($3 =="*") {
		print a" "b;
			}
		 else {
		print a " down";
		}
	
}}
}
IP FILE:

2015-02-28 10:10:17 M KDA003X
2015-02-28 10:12:06 * KDA003X
2015-02-28 10:16:50 M KDA003X
2015-02-28 10:10:17 M KDA003Y
2015-02-28 10:12:06 * KDA003Y
2015-02-28 10:16:50 M KDA003Y
2015-02-28 10:10:17 M KDA003Z
2015-02-28 10:12:06 * KDA003Z
2015-02-28 10:16:50 M KDA003Z
2015-02-28 10:10:15 M RTE001X
2015-02-28 10:12:06 * RTE001X
2015-02-28 10:16:50 M RTE001X
2015-02-28 10:18:30 * RTE001X
2015-02-28 10:10:15 M RTE001Y
2015-02-28 10:12:06 * RTE001Y
2015-02-28 10:16:50 M RTE001Y
2015-02-28 10:18:30 * RTE001Y
2015-02-28 10:10:15 M RTE001Z
2015-02-28 10:12:06 * RTE001Z
2015-02-28 10:16:50 M RTE001Z
2015-02-28 10:18:30 * RTE001Z
OP NEEDED:
2015-02-28 10:10:17 M KDA003X 2015-02-28 10:12:06 * KDA003X
2015-02-28 10:16:50 M KDA003X DOWN
2015-02-28 10:10:17 M KDA003Y 2015-02-28 10:12:06 * KDA003Y
2015-02-28 10:16:50 M KDA003Y DOWN
2015-02-28 10:10:17 M KDA003Z 2015-02-28 10:12:06 * KDA003Z
2015-02-28 10:16:50 M KDA003Z DOWN
2015-02-28 10:10:15 M RTE001X 2015-02-28 10:12:06 * RTE001X
2015-02-28 10:16:50 M RTE001X 2015-02-28 10:18:30 * RTE001X
2015-02-28 10:10:15 M RTE001Y 2015-02-28 10:12:06 * RTE001Y
2015-02-28 10:16:50 M RTE001Y 2015-02-28 10:18:30 * RTE001Y
2015-02-28 10:10:15 M RTE001Z 2015-02-28 10:12:06 * RTE001Z
2015-02-28 10:16:50 M RTE001Z 2015-02-28 10:18:30 * RTE001Z

Script i am using:

{
if ($3 =="M") {
a=$0;
	{
		getline;
		b=$2;
	if ($3 =="*") {
		print a" "b;
			}
		 else {
		print a " down";
		}
	
}}
}

Thanks in advance.

Rgards
Jaydeep Sadaria

Try:

awk '
$3 == "M" {
	if(m)	print ll, "DOWN"
	ll = $0
	m = 1
}
$3 == "*" {
	print ll, $0
	m = 0
}
END {	if(m)	print ll, "DOWN"
}' "IP FILE"
1 Like

Thanks dear... can u just brief me some what about code so that it can be useful to me in further. How it compare?

Regards
Jaydeep

It won't work that way, because if a getline gets a M record, it will not get processed..

Try this adaptation of my post in Find: filename in every subdirectory matching a pattern

awk ' 
  $3=="M" {
    if(up_records)
      for(i in DOWN) {
        print i, DOWN, (i in UP)?UP:"Down Down"
        delete UP
        delete DOWN
      }
      up_records=0
      DOWN[$4]=$0
  } 
  $3=="*" {
    UP[$4]=$0
    up_records=1
  }
  END {
    for(i in DOWN) {
      print i, DOWN, (i in UP)?UP:"Down Down"
    }
  }
' file

Output:

KDA003X 2015-02-28 10:10:17 M KDA003X 2015-02-28 10:12:06 * KDA003X
KDA003X 2015-02-28 10:16:50 M KDA003X Down Down
KDA003Y 2015-02-28 10:10:17 M KDA003Y 2015-02-28 10:12:06 * KDA003Y
KDA003Y 2015-02-28 10:16:50 M KDA003Y Down Down
KDA003Z 2015-02-28 10:10:17 M KDA003Z 2015-02-28 10:12:06 * KDA003Z
KDA003Z 2015-02-28 10:16:50 M KDA003Z Down Down
RTE001X 2015-02-28 10:10:15 M RTE001X 2015-02-28 10:12:06 * RTE001X
RTE001X 2015-02-28 10:16:50 M RTE001X 2015-02-28 10:18:30 * RTE001X
RTE001Y 2015-02-28 10:10:15 M RTE001Y 2015-02-28 10:12:06 * RTE001Y
RTE001Y 2015-02-28 10:16:50 M RTE001Y 2015-02-28 10:18:30 * RTE001Y
RTE001Z 2015-02-28 10:10:15 M RTE001Z 2015-02-28 10:12:06 * RTE001Z
RTE001Z 2015-02-28 10:16:50 M RTE001Z 2015-02-28 10:18:30 * RTE001Z

Is this enough?

awk '
$3 == "M" {	# For each input line with "M" in the 3rd field...
	# If the previous line was an "M" line, note that no "*" line was found.
	if(m)	print ll, "DOWN"
	# Save the contents of this line, and set "m" to indicate that this
	# line is an "M" line.
	ll = $0
	m = 1
}
$3 == "*" {	# For each input line with "*" in the 3rd field...
	# Print the previus line and the current line.
	print ll, $0
	# Set "m" to indicate that we do not have any saved line.
	m = 0
}
END {	# We have hit end of input...
	# If we have a saved "M" line, print it...
	if(m)	print ll, "DOWN"
}' "IP FILE"	# End the script and specify the input file to be processed.