Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



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

Reply
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #1  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Remove if the above line matches pattern

but keep if does not

I have a file: --> my.out


Code:
foo: bar
foo: moo
blarg
i am on vacation
foo: goose
foo: lucy
foo: moose
foo: stucky
groover@monkey.org
foo: bozo
grimace@gonzo.net
dear sir - blargo blargo
foo: goon
foo: sloppy
foo: saudi
gimme gimme gimme
i have something for you
foo: mad
foo: happy
foo: foo

I want:

Code:
blarg
i am on vacation
foo: goose

groover@monkey.org
foo: bozo

grimace@gonzo.net
dear sir - blargo blargo
foo: goon

gimme gimme gimme
i have something for you
foo: mad

Perhaps, if foo: xxx is not above, keep foo: xxx and any other lines not containing foo: ?

Can sed help me? I tried this:

Code:
sed '/foo: /{
        N
        /.*/D
        }' my.out

but it didn't totally do it ...

Thanks for any help!

Last edited by scottn; 03-10-2010 at 04:29 PM.. Reason: Please use code tags
Sponsored Links
  #2  
Old 03-10-2010
Registered User
 

Join Date: Dec 2009
Posts: 705
Thanks: 6
Thanked 56 Times in 54 Posts
Hi, spacegoose:


Code:
$ cat data
foo: bar
foo: moo
blarg
i am on vacation
foo: goose
foo: lucy
foo: moose
foo: stucky
groover@monkey.org
foo: bozo
grimace@gonzo.net
dear sir - blargo blargo
foo: goon
foo: sloppy
foo: saudi
gimme gimme gimme
i have something for you
foo: mad
foo: happy
foo: foo

Using SED:

Code:
$ cat spacegoose.sed
#n
/^foo:/!{
    :top
    p
    /^foo:/!{
        n
        b top
    }
}

$ sed -f spacegoose.sed data
blarg
i am on vacation
foo: goose
groover@monkey.org
foo: bozo
grimace@gonzo.net
dear sir - blargo blargo
foo: goon
gimme gimme gimme
i have something for you
foo: mad

If you require a blank line after each sequence:

Code:
$ cat spacegooseLF.sed
#n
/^foo:/!{
    :top
    p
    /^foo:/!{
        n
        b top
    }
    s/.*//p
}

$ sed -f spacegooseLF.sed data
blarg
i am on vacation
foo: goose

groover@monkey.org
foo: bozo

grimace@gonzo.net
dear sir - blargo blargo
foo: goon

gimme gimme gimme
i have something for you
foo: mad

Using AWK:

Code:
awk '!/^foo:/,/^foo/'

If you require a blank line after each sequence:

Code:
awk '!/^foo:/,/^foo/{print; if(/^foo/)print ""}'

Cheers,
Alister

Last edited by alister; 03-10-2010 at 04:11 PM..
  #3  
Old 03-10-2010
Registered User
 

Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Alister,

The first example works (thanks , but the second one yields:


Code:
awk: syntax error near line 1
awk: illegal statement near line 1

I'm using Solaris.

Last edited by scottn; 03-10-2010 at 04:30 PM.. Reason: Code tags
  #4  
Old 03-10-2010
scottn's Avatar
scottn scottn is offline Forum Staff  
Moderator
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 2,885
Thanks: 23
Thanked 135 Times in 131 Posts
Hi.

On Solaris, use /usr/xpg4/bin/awk, or nawk
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl line count if it matches a pattern nmattam Shell Programming and Scripting 2 09-28-2009 10:50 AM
Print word 1 in line 1 and word 2 in line 2 if it matches a pattern bangaram Shell Programming and Scripting 7 08-31-2009 05:58 AM
Need to remove few characters from each line till a pattern is matched kiranlalka Shell Programming and Scripting 4 05-19-2009 03:31 AM
shell script to remove all lines from a file before a line starting with pattern raksha.s Shell Programming and Scripting 2 03-29-2009 07:13 AM
Print line if first Field matches a pattern Raynon Shell Programming and Scripting 2 01-08-2009 05:07 AM



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