How to print previous line of multiple pattern matched line?

Hello,

I have below format log file,

Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv
Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and csv_converted_files/224517/97447.1012H67126.5077292103609547345.csv
Comparing csv_converted_files/0520/7447.1012H67126.5077292103609547345.csv and csv_converted_files/22004517/97447.1012H67126.5077292103609547345.csv
Line 1508;Field 3 diff value 14.983798027 - 14.9837970734 =  9.536e-07
Comparing csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341918945 - -260.341888428 =  -3.0517e-05
Comparing csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341888428 - -260.341918945 =  3.0517e-05
Comparing csv_converted_files/520151130004443/24740.48537131001.16216540273753330633.csv and csv_converted_files/720151130006698/24740.48537131001.16216540273753330633.csv
Line 1295;Field 103 diff value -1.57253694534 - -1.57253658772 =  -3.5762e-07
Line 1378;Field 64 diff value 5.29786920547 - 5.29786968231 =  -4.7684e-07

I wanted to print line starting with ^Line and having diff value greater than > 10.000
Along with it previous line starting with ^Comapring string. So basically a line starting with ^Comparing can have multiple corresponding line starting with ^Line .

grep -B1 gives previous line only if single line having ^Line is present but however with multiple lines having ^Line doesn't work.

Any help appreciated and let me know if any further information is required.

Thanks,
Arvind.

It is not clear what you mean with diff value, is that field 6 or field 10. Also is the difference only >10 or also <-10 :
Here is an example you could try with $6 and both negative and positive, to give you an idea of how this might be approached:

$ awk '$1=="Comparing"{p=$0} $1=="Line" && ($6>10 || $6<-10) {if(p){print p; p=x} print}' infile
Comparing csv_converted_files/0520/7447.1012H67126.5077292103609547345.csv and csv_converted_files/22004517/97447.1012H67126.5077292103609547345.csv
Line 1508;Field 3 diff value 14.983798027 - 14.9837970734 =  9.536e-07
Comparing csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341918945 - -260.341888428 =  -3.0517e-05
Comparing csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341888428 - -260.341918945 =  3.0517e-05

There's not a single line in your sample having a diff value > 10.000, so it's difficult to test. Tested with 1E-6 instead (replace with 1E2 for your values). Try

awk '/^Line/ && ($NF*$NF > 1E-12) && C {print LAST; print} {C = /^Comp/; LAST = $0}' file
Comparing csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341918945 - -260.341888428 =  -3.0517e-05
Comparing csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341888428 - -260.341918945 =  3.0517e-05

Given that no line in your sample data starting with Line has a computed diff value greater than .000030517, your are requesting software that will produce no output with your sample data.

It isn't clear from your description whether or not you would consider -11 to be greater than 10. Are you looking for an absolute value greater than 10 or an arithmetic value greater than 10?

Can you show us some sample data AND the output you are trying to produce from that data where the expected output would not be an empty file (with both the input data and the output data in CODE tags)?

What operating system and shell are you using?

To clarify what Output I want let me give example, check will be on $10,

I/P :
E.g :

Comparing csv_converted_files/220151130005009/97447.101866J362.11513249717775333748.csv and csv_converted_files/220151130005008/97447.101866J362.11513249717775333748.csv
Line 1493;Field 42 diff value -260.341888428 - -260.341918945 =  3.0517e-05
Comparing csv_converted_files/520151130004443/24740.48537131001.16216540273753330633.csv and csv_converted_files/720151130006698/24740.48537131001.16216540273753330633.csv
Line 1295;Field 103 diff value -1.57253694534 - -1.57253658772 =  -3.5762e-07
Line 1378;Field 64 diff value 5.29786920547 - 5.29786968231 =  10.090909

So O/P I want,

Comparing csv_converted_files/520151130004443/24740.48537131001.16216540273753330633.csv and csv_converted_files/720151130006698/24740.48537131001.16216540273753330633.csv
Line 1378;Field 64 diff value 5.29786920547 - 5.29786968231 =  10.090909

Not sure how 5.29786920547 - 5.29786968231 can result in 10.090909, but try

awk '/^Line/ && ($NF > 10) {print LAST; print} /^Comp/ {LAST = $0}' file
Comparing csv_converted_files/520151130004443/24740.48537131001.16216540273753330633.csv and csv_converted_files/720151130006698/24740.48537131001.16216540273753330633.csv
Line 1378;Field 64 diff value 5.29786920547 - 5.29786968231 = 10.090909

Try:

awk '$1=="Comparing"{p=$0} $1=="Line" && $NF>10 {if(p){print p; p=x} print}'  infile

--
@RudiC that script will print the "Comparing" line for every line with value > 10, so if there are two > 10 then the Comparing line will be printed twice

1 Like