Remove if the above line matches pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove if the above line matches pattern
# 1  
Old 03-10-2010
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 Scott; 03-10-2010 at 05:29 PM.. Reason: Please use code tags
# 2  
Old 03-10-2010
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 05:11 PM..
# 3  
Old 03-10-2010
Thanks Alister,

The first example works (thanks Smilie, 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 Scott; 03-10-2010 at 05:30 PM.. Reason: Code tags
# 4  
Old 03-10-2010
Hi.

On Solaris, use /usr/xpg4/bin/awk, or nawk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get a 1st line which matches the particular pattern?

Hi all, I have file on which I do grep on "/tmp/data" then I get 5 lines as dir Path: /tmp/data/20162343134 Starting to listen on ports logging: -- Moving results files from local storage: /tmp/resultsFiles/20162343134/*.gz to NFS: /data/temp/20162343134/outgoing from above got to get... (7 Replies)
Discussion started by: girijajoshi
7 Replies

2. Shell Programming and Scripting

Remove entire line from a file if 1st column matches a pattern

I have one requirement to delete all lines from a file if it matches below scenario. File contains three column. Employee Number, Employee Name and Employee ID Scenario is: delete all line if Employee Number (1st column) contains below 1. Non-numeric Employee Number 2. Employee Number that... (3 Replies)
Discussion started by: anshu ranjan
3 Replies

3. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

5. Shell Programming and Scripting

[Solved] Finding the next line when a pattern matches

Hi I have a file like this Record 182: Rejected No Data found Record 196: Rejected File Not Found Record 202: Rejected Invalid argument Record 212: Rejected Bad data My requirement is to search for the value "Record" and if found, then return the next line of it. So,... (3 Replies)
Discussion started by: mr_manii
3 Replies

6. Shell Programming and Scripting

remove contents including the tag if pattern matches

Hi all, Can anyone help me on this. I have several WP sites that are affected by sql injections. But the contents are different as follows western union india belgaum western union india bolegaon western union india barhaj western union india budhana western union india belda western... (6 Replies)
Discussion started by: sanjuabraham
6 Replies

7. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

8. Shell Programming and Scripting

Perl line count if it matches a pattern

#!/usr/bin/perl use Shell; open THEFILE, "C:\galileo_integration.txt" || die "Couldnt open the file!"; @wholeThing = <THEFILE>; close THEFILE; foreach $line (@wholeThing){ if ($line =~ m/\\0$/){ @nextThing = $line; if ($line =~ s/\\0/\\LATEST/g){ @otherThing =... (2 Replies)
Discussion started by: nmattam
2 Replies

9. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

10. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies
Login or Register to Ask a Question