Compare between current and next line and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare between current and next line and print
# 1  
Old 03-06-2015
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:

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


Code:
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

Code:
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:

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


Thanks in advance.

Rgards
Jaydeep Sadaria
# 2  
Old 03-06-2015
Try:
Code:
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"

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-06-2015
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
# 4  
Old 03-06-2015
Quote:
Originally Posted by jaydeep_sadaria
Dear All
[..]

Script i am using:

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

[..]
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

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

Output:
Code:
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

# 5  
Old 03-06-2015
Quote:
Originally Posted by jaydeep_sadaria
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
Is this enough?
Code:
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.


Last edited by Don Cragun; 03-06-2015 at 10:30 PM.. Reason: Fix typo (s/thee/the/).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tput cup, print on current line?

Heyas I'm thinking about a new approach for my core display, basicly as it should make aligments easier. Issue i'm currently facing, is tput cup capable of printing on the current line? My best achievements were: :) tui $ tput cup - 60;echo " ------ testing" ------ testing... (5 Replies)
Discussion started by: sea
5 Replies

2. Shell Programming and Scripting

How to compare the current result with previous line result.?

Hi Gurus, I have requirement to compare current result with previous reuslt. The sample case is below. 1 job1 1 1 job2 2 1 job3 3 2 job_a1 1 2 job_a2 2 2 job_a3 3 3 job_b1 1 3 job_b2 2 for above sample file, GID is group ID, for input line, the job run... (1 Reply)
Discussion started by: ken6503
1 Replies

3. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

4. UNIX for Dummies Questions & Answers

Awk to print data from current and previous line

Hi guys, I have found your forum super useful. However, right now I am stuck on a seemingly "simple" thing in AWK. I have two columns of data, the first column in Age (in million years) and the second column is Convergence Rate (in mm/yr). I am trying to process my data so I can use it to... (2 Replies)
Discussion started by: awk_noob_456
2 Replies

5. Shell Programming and Scripting

Compare selected columns of two files and print whole line with mismatch

hi! i researched about comparing two columns here and got an answer. but after examining my two files, i found out that the first columns of the two files are not unique with each other. all i want to compare is the 2nd and 3rd column. FILE 1: ABS 456 315 EBS 923 163 JYQ3 654 237 FILE 2:... (1 Reply)
Discussion started by: engr.jay
1 Replies

6. Shell Programming and Scripting

Compare two file and print same line

i want to compare two file and print same line file1 12345 a 23456 a 45678 a 45679 a file2 23456 a 34567 a 45679 a output 23456 a 45679 a any one can help me? Thank you (7 Replies)
Discussion started by: bleach8578
7 Replies

7. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

8. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies
Login or Register to Ask a Question