Print the previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the previous line
# 1  
Old 07-08-2010
Print the previous line

My requirement is that when ever search criteria matchs in log file, the previous line just above the search word as well as search word should be print.

sample log file

Code:
--03-19T11:26  xxx    create version "a.sh@@/main/6"
  "104157 "
--03-18T16:01  xxx    create version "a.sh@@/main/5"
  "104157 "
--03-16T12:51  xxx    create version "a.sh@@/main/4"
  "874569"
--03-09T17:49  xxx    create version "a.sh@@/main/3"
  "874569"
--02-23T07:21  xxx  create version b.sh@@/main/14
  "94145"
--02-22T07:12  xxx  create version "b.sh@@/main/13"
  "94145"
--02-21T00:10  xxx    create version "b.sh@@/main/12"
  "2010"



No if i search for 94145, then output shoul be -

Code:
--02-23T07:21  xxx  create version b.sh@@/main/14    <-- previous line   
  "94145"                                                              <-- search critria
--02-22T07:12  xxx  create version b.sh@@/main/13   <-- previous line   
  "94145"                                                  <-- search critria


another example, if user search for 2010, then output should be -


Code:
--02-21T00:10  xxx    create version "b.sh@@/main/12"
  "2010"

Any kind of help will be appriciated.

Thanks in advance.

Last edited by jadoo_c2; 07-08-2010 at 09:44 AM..
# 2  
Old 07-08-2010
Code:
awk '$0 ~ v { print p RS $0 }
{ p = $0 }' v=94145 infile

And of course, you may need to make the search more specific.
# 3  
Old 07-08-2010
Thanks!!

Can you please tell me should i use your suggestion as a awk script or as a command ??

---------- Post updated at 06:20 PM ---------- Previous update was at 06:19 PM ----------

Can yopu please ellobrate it more??
# 4  
Old 07-08-2010
Hi, Try this
Code:
grep -B1 "94145" testfile

# 5  
Old 07-08-2010
Quote:
Originally Posted by pravin27
Hi, Try this
Code:
grep -B1 "94145" testfile


Consider that -A/B are supported only by GNU grep.

---------- Post updated at 02:57 PM ---------- Previous update was at 02:56 PM ----------

Quote:
Originally Posted by jadoo_c2
Thanks!!

Can you please tell me should i use your suggestion as a awk script or as a command ??

---------- Post updated at 06:20 PM ---------- Previous update was at 06:19 PM ----------

Can yopu please ellobrate it more??
Just run the script on the command line:

Code:
awk '$0 ~ v { print p RS $0 } { p = $0 }' v=94145 <your_log_file_name>

# 6  
Old 07-08-2010
I tried what you said and got following error message

Code:
 
$ awk '$0 ~ v { print p RS $0 } { p = $0 }' v=94145 log
awk: syntax error near line 1
awk: bailing out near line 1
$

Please remember that i am using K shell and Sun Solaries as OS
# 7  
Old 07-08-2010
OK, just use nawk instead of awk.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk or sed to print the character from the previous line after the regexp match

Hi All, I need to print the characters in the previous line just before the regular expression match Please have a look at the input file as attached I need to match the regular expression ^ with the character of the previous like and also the pin numbers and the output file should be like... (6 Replies)
Discussion started by: kshitij
6 Replies

2. AIX

Print nth previous line after match

Please help me print nth line after match awk or sed one line command. (3 Replies)
Discussion started by: sushma123
3 Replies

3. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

5. 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

6. Shell Programming and Scripting

Print a field from the previous line

plz help me!! I have this file , 3408 5600 3796 6035 4200 6285 4676 0 40 1554 200 1998 652 2451 864 2728 1200 0 I want it like if $2==0,replace it with field from the previous line+500 say here the o/p would be like 3408 5600 3796 6035 4200 6285... (16 Replies)
Discussion started by: Indra2011
16 Replies

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

8. Shell Programming and Scripting

how Print previous line ..........

HELLO...I wanted to ask you, than sure know unix more than me, as I can obtain the following solution: I have a file with rows of the type: CIAO COME STAI PERCHE COME STAI CIAO COME VA ALLO CHE FACCIAMO ................. I would that if in a line is present the word (for example) " CHE... (9 Replies)
Discussion started by: fabi20
9 Replies

9. 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

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