Visit Our UNIX and Linux User Community

sed: delete regex line and next line if blank

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: delete regex line and next line if blank
# 1  
Old 09-18-2008
sed: delete regex line and next line if blank


I want to write a sed script which from













I would imagine something like:
"if a line containing ":" is followed by an empty line delete both lines (the line with ":" and the empty line)"
i.e. something like in general
if a line containing regex1 is followed (immediately after) by a line containing regex2 delete both lines.

if it would be on one line I would do: sed '/regex1.*regex2/d' -->
how to spread this command on 2 lines? And consider hat in my case regex2 is an empty line ( i.e. ^$).

# 2  
Old 09-18-2008
oh yes! this works now!
Thanks a lot and thanks for the explanation too

Last edited by one71; 09-18-2008 at 07:59 AM..
# 3  
Old 09-18-2008
Sorry, I posted my first reply based on a too cursory reading of your question.

sed '/:$/!b;N;/:\n$/d' input.txt

Here's a brief explanation:

/:$/!b - if not a line ending with a colon, just skip to the end of the script and print.
N - this is a line ending with a colon; fetch the next line and glue them together.
/:\n$/d - if the combined two lines match this pattern, delete
else, print

The \n thing works differently in different versions of sed, but if it doesn't work, try with a literal newline, with or without a backslash.

Last edited by era; 09-18-2008 at 07:56 AM.. Reason: Note on \n in different versions of sed

Previous Thread | Next Thread
Test Your Knowledge in Computers #376
Difficulty: Medium
HP-UX was UNIX V7 compliant.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to delete blank line/s before and after a search pattern?

Hi, Test file x.txt below. This file is generated by a program that I unfortunately do not have control on how it gets presented/generated. create PACKAGE "XXX_INTERFACE_DEFECT_RPT_TEST" is TYPE refCursor IS REF CURSOR; Function queryRecords ( p_status varchar2, ... ... ... )... (4 Replies)
Discussion started by: newbie_01
4 Replies

2. Shell Programming and Scripting

Regex to include up to blank line.

Hi guys I am trying to figure out how to match a pattern with a regex up to a full blank line. I will show you what I mean with this example: example A movie name: ted movie name: TMNT movie name: Jinxed example B movie names: Gravity Faster Turbo song titles: dont hello problem (8 Replies)
Discussion started by: acoding
8 Replies

3. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

4. 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

5. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

6. Shell Programming and Scripting

delete blank line from middle of a file

All, I have a file which contains two entry with spaces (either one or more than one space). ex. /tmp/scripts/sql CUST_YR_END_INI.sql /tmp/scripts/sql CUST_WK_END_INI.sql /tmp/scripts/sql CUST_MTH_END_INI.sql /tmp/scripts/sql CUST_YR_END_INC.sql now I want to... (11 Replies)
Discussion started by: anshu ranjan
11 Replies

7. Shell Programming and Scripting

Need sed help: find regex and if the next next line is blank, delete both

I've got a report I need to make easier to read Using sh on HP-UX 11.12. In short, I want to search for a regular expression and when found, examine the next line to see if it's blank. If so, then delete both lines. If not blank, move on to the next regexp. Repeat. So far I've got: ... (7 Replies)
Discussion started by: Scottie1954
7 Replies

8. Shell Programming and Scripting

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:... (2 Replies)
Discussion started by: rocketman88
2 Replies

9. Shell Programming and Scripting

Delete last blank line.

I need to delete the last line only if its blank not otherwise. (3 Replies)
Discussion started by: dinjo_jo
3 Replies

10. Shell Programming and Scripting

how to delete a first blank line from the file

I have a file which has the first blank line: sundev22$cat /t1/bin/startallocs /t1/bin/startallocsys 123 sundev22$ Is there a command to remove this first blank line? Thanks for help -A (4 Replies)
Discussion started by: aoussenko
4 Replies

Featured Tech Videos