Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Delete lines with a word and their above lines

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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:
Delete lines with a word and their above lines Use code tags please, see PM.

Last edited by zaxxon; 07-19-2013 at 10:46 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 07-19-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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 Unix and Linux 07-19-2013
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 2 December 2016, 11:31 PM EST
Location: Chennai
Posts: 2,273
Thanks: 491
Thanked 1,032 Times in 939 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 Unix and Linux 07-19-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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 Unix and Linux 07-19-2013
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 2 December 2016, 11:31 PM EST
Location: Chennai
Posts: 2,273
Thanks: 491
Thanked 1,032 Times in 939 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. Linux



Thanks,
R. Singh
Sponsored Links
    #6  
Old Unix and Linux 07-19-2013
Manish00712 Manish00712 is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 July 2015, 1:00 PM EDT
Posts: 5
Thanks: 2
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 Unix and Linux 07-19-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 December 2016, 2:43 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 Posts
Use nawk instead in Solaris or SunOS
Sponsored Links
 

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
search and replace, when found, delete multiple lines, add new set of lines? DeuceLee Shell Programming and Scripting 3 11-23-2011 04: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 04:36 AM
How to delete all lines before a specific word? hakermania Shell Programming and Scripting 2 07-23-2010 03:37 PM
search a word and delete consecutive lines below it vikas027 Shell Programming and Scripting 3 06-03-2009 03:02 AM
delete lines containing a specific word in afile sireesha9 Shell Programming and Scripting 4 02-23-2009 06:07 AM



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