Previous Line 
Author Message
 Previous Line

Hi !!!

How do I print previous line in awk/sed if pattern is matched.

Example :<Data File>

Test Line 1
Test Line A
Test Line B
Test Line 4
Test Line C
Test Line B
Test Line X
Test Line B

if my pattern is 'Test Line B' output should be

Test Line A
Test Line C
Test Line X

Thanks in advance

- H



Mon, 21 Feb 2005 14:07:14 GMT  
 Previous Line

Quote:
> Hi !!!

> How do I print previous line in awk/sed if pattern is matched.

> Example :<Data File>

> Test Line 1
> Test Line A
> Test Line B
> Test Line 4
> Test Line C
> Test Line B
> Test Line X
> Test Line B

> if my pattern is 'Test Line B' output should be

> Test Line A
> Test Line C
> Test Line X

> Thanks in advance

> - H

Just remember the last line.

awk ' \
    /^Test Line B$/ {print last} \
    { last = $0 } \
'

    - Dan



Mon, 21 Feb 2005 14:18:22 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. how to grep previous line in file ?

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. 'gets' in reverse/previous line?

7. My previous

8. Correction of my previous posting.

9. Handle of previous (non-dolphin) Window

10. Contents of previous patches?

11. Previous Bankruptcies or Foreclosures OK

12. GF/ST namespace changed from previous version

 

 
Powered by phpBB® Forum Software