Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Any solution to get previous line in awk

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-18-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 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 Unix and Linux 08-18-2013
kshji's Unix or Linux Image
kshji kshji is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 26 July 2015, 6:22 AM EDT
Location: Finland
Posts: 622
Thanks: 0
Thanked 54 Times in 53 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 Unix and Linux 08-18-2013
Akshay Hegde's Unix or Linux Image
Akshay Hegde Akshay Hegde is offline Forum Advisor  
Forum Advisor
 
Join Date: Nov 2012
Last Activity: 27 July 2015, 5:19 AM EDT
Location: Goa / Bangalore, India
Posts: 1,732
Thanks: 85
Thanked 586 Times in 533 Posts
Thanks a lot...
    #4  
Old Unix and Linux 08-18-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
Prints line before pattern

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

Sponsored Links
    #5  
Old Unix and Linux 08-18-2013
krishmaths krishmaths is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 6 July 2015, 5:32 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 Unix and Linux 08-19-2013
Jotne's Unix or Linux Image
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:08 PM.