Delete lines with a word and their above lines | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Delete lines with a word and their above lines

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2013
the_simpsons the_simpsons is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 26 August 2014, 9:22 PM EDT
Posts: 35
Thanks: 2
Thanked 1 Time in 1 Post
Delete lines with a word and their above lines

Hi, i have a file like this:


Code:
A1
kdfjdljfdkljfdlf

A2
lfjdlfkjddkjf

A3
***no hit***

A4
ldjfldjfdk

A5
***no hit***

A6
jldfjdlfjdlkfjd

I want to remove the lines "***no hit*** and their above line to get an output file like this:

Code:
A1
kdfjdljfdkljfdlf

A2
lfjdlfkjddkjf

A4
ldjfldjfdk

A6
jldfjdlfjdlkfjd

Thanks!!


Moderator's Comments:
Use code tags please, see PM.

Last edited by zaxxon; 07-19-2013 at 09:46 AM.. Reason: code tags
Sponsored Links
    #2  
Old 07-19-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts

Code:
awk 'NF{v=$0;getline;if($0!="***no hit***") print v RS $0 RS " "}' file

The Following User Says Thank You to Yoda For This Useful Post:
RavinderSingh13 (07-19-2013)
Sponsored Links
    #3  
Old 07-19-2013
RavinderSingh13 RavinderSingh13 is online now
Registered User
 
Join Date: May 2013
Last Activity: 24 October 2014, 5:10 AM EDT
Location: Chennai
Posts: 740
Thanks: 207
Thanked 164 Times in 159 Posts
Quote:

Code:
awk 'NF{v=$0;getline;if($0!="***no hit***") print v RS $0 RS " "}' file

Hello Yoda,

Sorry to bother you, could you please explaoin this great command of awk.
I will be grateful to you.


Thanks,
R. Singh
    #4  
Old 07-19-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts

Code:
awk '
        # If NF ( Number of Fields ) not equal to zero
        NF {
                # Set variable v = $0 ( current record )
                v = $0
                # Explicitly read next input record
                getline
                # If $0 ( current record ) != "***no hit***"
                if ( $0 != "***no hit***" )
                        # Print variable v value RS ( Record Separator - newline by default ) $0 RS blank space " "
                        print v RS $0 RS " "
        }
' file

The Following User Says Thank You to Yoda For This Useful Post:
RavinderSingh13 (07-19-2013)
Sponsored Links
    #5  
Old 07-19-2013
RavinderSingh13 RavinderSingh13 is online now
Registered User
 
Join Date: May 2013
Last Activity: 24 October 2014, 5:10 AM EDT
Location: Chennai
Posts: 740
Thanks: 207
Thanked 164 Times in 159 Posts
Thanks a lot Yoda, I have no hesitation to say you are one of the champs for awk. It's really pleasure to read/follow your posts.



Thanks,
R. Singh
Sponsored Links
    #6  
Old 07-19-2013
Manish00712 Manish00712 is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 20 July 2013, 6:47 AM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Yoda,

I just tried your awk script but i get an error @ line 3 i.e v=$0. As i'm pretty new to awk i could not make out what could be the problem. And also one more doubt what i have is:

Ravinder wanted to delete the line before the ***no hit*** also but with the logic what i understood the below script will print the line A3 and then check for ***no hit*** condition rite..?


bash-3.00# awk '
> NF
> {
> v=$0
> getline
> if ($0!="***no hit***")
> print v RS $0 RS " "
> '
awk: syntax error near line 3
awk: bailing out near line 3
Sponsored Links
    #7  
Old 07-19-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 22 October 2014, 8:12 PM EDT
Location: Galactic Empire
Posts: 3,387
Thanks: 235
Thanked 1,209 Times in 1,135 Posts
Use nawk instead in Solaris or SunOS
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
search and replace, when found, delete multiple lines, add new set of lines? DeuceLee Shell Programming and Scripting 3 11-23-2011 03:39 PM
Search the word to be deleted and delete lines above this word starting from P1 to P3 vsachan Shell Programming and Scripting 2 05-24-2011 03:36 AM
How to delete all lines before a specific word? hakermania Shell Programming and Scripting 2 07-23-2010 02:37 PM
search a word and delete consecutive lines below it vikas027 Shell Programming and Scripting 3 06-03-2009 02:02 AM
delete lines containing a specific word in afile sireesha9 Shell Programming and Scripting 4 02-23-2009 05:07 AM



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