Any solution to get previous line in awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Any solution to get previous line in awk

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-18-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 12 April 2014, 10:51 PM EDT
Location: Goa, India
Posts: 1,190
Thanks: 54
Thanked 339 Times in 313 Posts
Any solution to get previous line in awk

Hi all...

Is there any solution to do the reverse process of getline ?

Thanks in advance.
Sponsored Links
    #2  
Old 08-18-2013
kshji's Avatar
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 February 2014, 5:44 AM EST
Location: Finland
Posts: 602
Thanks: 0
Thanked 50 Times in 49 Posts
Why not only save line to variable ?

Code:
{ 
  # save current line for later using
   prev=$0
}

If need previous line, you can use variable or return it to the variable $0

Code:
{ # some rule
     # save current for later using
     curr=$0
     # take previous line
     $0=prev
     # do something
     ...
     # and return current line
     $0=curr
}

Sponsored Links
    #3  
Old 08-18-2013
Akshay Hegde's Avatar
Akshay Hegde Akshay Hegde is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 12 April 2014, 10:51 PM EDT
Location: Goa, India
Posts: 1,190
Thanks: 54
Thanked 339 Times in 313 Posts
Thanks a lot...
    #4  
Old 08-18-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 Posts
Prints line before pattern

Code:
awk '/pattern/ {print a}{a=$0}'

Sponsored Links
    #5  
Old 08-18-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 20 March 2014, 4:46 AM EDT
Location: Chennai, India
Posts: 443
Thanks: 22
Thanked 70 Times in 68 Posts
Hi Jotne,

Could you please explain the code?

Thanks!
Sponsored Links
    #6  
Old 08-19-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 8 April 2014, 11:00 AM EDT
Posts: 1,036
Thanks: 61
Thanked 216 Times in 204 Posts

Code:
awk '
	/pattern/ {print a}
	{a=$0}'

Running on a file:
Line1: Does it contain "pattern", yes print a (here a will be empty), no continue
a=Line1
Line2: Does it contain "pattern", yes print a (her a contial line1), no continue
a=Line2
Line3: Does it contain "pattern", yes print a (her a contial line2), no continue
a=Line3

So a does always contain previous line.
The Following User Says Thank You to Jotne For This Useful Post:
krishmaths (08-19-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to insert line previous to a pattern? Drenhead Shell Programming and Scripting 2 04-24-2013 06:28 PM
awk script -print line when $2 > $2 of previous line gafoleyo73 Shell Programming and Scripting 2 03-05-2013 08:00 PM
Awk help with source and previous line loop Grueben Shell Programming and Scripting 4 10-10-2010 01:17 PM
awk get matched line's previous line jimmy_y Shell Programming and Scripting 11 08-19-2010 06:49 AM
awk;sed appending line to previous line.... walkerwheeler Shell Programming and Scripting 11 06-24-2009 07:59 PM



All times are GMT -4. The time now is 09:09 AM.