how to grep previous line in file ? 
Author Message
 how to grep previous line in file ?

getting nextline fine but how to get previous line ???

cat filename

1
2
date
4
date
3
6

awk '/date/ {print $0;getline;print $0}'  filename

date
4
date
3

need to get output from filename as below including date or just the line
before string date

of

2
date
4
date

Thanks in advance Sammy



Tue, 27 Jul 2004 12:56:18 GMT  
 how to grep previous line in file ?

Quote:

>getting nextline fine but how to get previous line ???

Keep it in a variable.
awk '/date/{print prevline;print $0} {prevline=$0}' filename


Tue, 27 Jul 2004 14:40:19 GMT  
 how to grep previous line in file ?

Quote:

>getting nextline fine but how to get previous line ???

>cat filename

>1
>2
>date
>4
>date
>3
>6

>awk '/date/ {print $0;getline;print $0}'  filename

>date
>4
>date
>3

>need to get output from filename as below including date or just the line
>before string date

>of

>2
>date
>4
>date

if there will always be a line before the date line, then this will
work:

awk '/date/ {print saved ; print $0} {saved=$0}' filename

basically you print what you've save when you need to, and you save
after you've made the printing decision, so that when you do print
you're printing the correct saved data.

Alternatively, GNU grep has this functionality.

Chuck Demas

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Tue, 27 Jul 2004 17:46:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Previous Line

2. Newbie needing help to print previous lines...

3. Inserting text from a previous line?

4. how to read previous line

5. reading previous line using expect

6. grep-like search with multiple file output?

7. grep pattern in files

8. set count [exec grep -c set $file]

9. Preventing using files from a previous project

10. Data files II (Disregard previous message from e.lim@auckland.ac.nz

 

 
Powered by phpBB® Forum Software