How to print previous line of multiple pattern matched line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print previous line of multiple pattern matched line?
# 1  
Old 02-04-2016
How to print previous line of multiple pattern matched line?

Hello,

I have below format log file,
Code:
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.

Last edited by Don Cragun; 02-04-2016 at 04:53 AM.. Reason: Get rid of Italic and COLOR tags; add CODE and ICODE tags.
# 2  
Old 02-04-2016
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:

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

# 3  
Old 02-04-2016
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
Code:
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

# 4  
Old 02-04-2016
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?
# 5  
Old 02-04-2016
To clarify what Output I want let me give example, check will be on $10,

I/P :
E.g :
Code:
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,

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

Moderator's Comments:
Mod Comment Please stop using color and italic tags to display sample input and output. Use CODE tags for sample input, sample output, and code segments.

Last edited by Don Cragun; 02-04-2016 at 06:53 AM.. Reason: Change Italic and COLOR tags to CODE tags again.
# 6  
Old 02-04-2016
Not sure how 5.29786920547 - 5.29786968231 can result in 10.090909, but try
Code:
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


Last edited by RudiC; 02-05-2016 at 05:06 AM..
# 7  
Old 02-04-2016
Try:
Code:
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
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

4. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

5. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

6. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

7. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

8. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

9. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

10. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies
Login or Register to Ask a Question