Sed or Grep to delete line containing patter plus extra line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed or Grep to delete line containing patter plus extra line
# 1  
Old 06-29-2010
Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out:

Delete every line containing the word CEN and the next line as well.
ie. test.txt

blue
324 CEN
green
red
blue
324 CEN
green
red
blue

to produce:

blue
red
blue
red
blue


I can delete the first line using either:

Code:
sed '/CEN/ d' test.txt

or
Code:
grep -v 'CEN' test.txt

I tried to extend this with sed by adding
Code:
 
sed '/CEN/,2d' test.txt

But this didn't work as well.
There is nothing to use as a marker in the second line I want to delete so I can't run two of these commands in series.

Any help would be greatly appreciated.
# 2  
Old 06-30-2010
Hi

Code:
sed '/CEN/{N;d;}' test.txt

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 06-30-2010
Thanks

Works well, thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Delete line breaks and extra spaces between patterns

Hi, if in between strings "<section" and "</section>" across multiple lines there occurs the string "ole-present", delete all line breaks and replace any tabs or multiple spaces with a single space. Looking for an AWK or SED solution. Thank you. <section ... status = "ole-present" ...... (2 Replies)
Discussion started by: pioavi
2 Replies

5. Shell Programming and Scripting

how to delete extra character in a line?

And I want to delete the characters longer than 20 for each line start with #. The other lines should remain the same. I think this can be done by sed. Could anyone help me with this? Thanks! my input file: #ZP_05494889.1_Clostridium_papyrosolvens... (3 Replies)
Discussion started by: ritacc
3 Replies

6. Shell Programming and Scripting

USING sed to delete a line

Please let me know wat would be sed command to delete any partcular line from a file and also moving lines below it to up. ie wen line #9 is deleted data in line #10 should move to #9 and so on. (2 Replies)
Discussion started by: fidelis
2 Replies

7. Shell Programming and Scripting

'sed' command shows extra line

Here is an interesting problem that I am seeing when using 'sed'. The goal is to print out the names of the Oracle SID's on a system and here is the command I am using: ps -ef|grep ora|grep pmon|awk '{print $NF}'|sed 's/ora_pmon_//' The output of the command after the awk is this: ... (3 Replies)
Discussion started by: soleil4716
3 Replies

8. Shell Programming and Scripting

Grep a string in a range and delete the line

Hi, i need to delete a lines after searching a particular string but this searching should only happen after the 4th line.. basically imagine a file like this From: abcd.yahoo.com To: cdeb.yahoo.com Subject: hi all sdfsd sadasd asdasd dfsdf From: abcd.yahoo.com To:... (3 Replies)
Discussion started by: depakjan
3 Replies

9. UNIX for Advanced & Expert Users

Inserting a line before the line which matches the patter

Hi Is there any command where we can insert a line "2|||" before every line starting with "3|" my input is as follows 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 3|79.58|||GBP|| 1|ETG|12345 2|EN_GB||Electrogalvanize 0.5 m2 ( Renault ) 1|ETG|12345 3|88.51|||GBP|| desired output... (10 Replies)
Discussion started by: laxmi131
10 Replies

10. Shell Programming and Scripting

sed: delete regex line and next line if blank

Hi, I want to write a sed script which from batiato: batiato/giubbe: pip_b.2.txt pip_b.3.txt pip_b.3mmm.txt bennato: bennato/peterpan: 123.txt consoli: pip_a.12.txt daniele: (2 Replies)
Discussion started by: one71
2 Replies
Login or Register to Ask a Question