|
google site
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
![]() |
|
|
Search this Thread |
|
#1
|
|||
|
|||
|
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.outbut 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
|
|||
|
|||
|
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: madIf 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: madUsing 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
|
|||
|
|||
|
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
|
||||
|
||||
|
Hi.
On Solaris, use /usr/xpg4/bin/awk, or nawk |
| Sponsored Links | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |