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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need sed help: find regex and if the next next line is blank, delete both
# 1  
Old 07-28-2011
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:

Code:
sed -n -e '/[0-9]-[0-9][0-9]/p' infile >outfile

That finds my regular expression (which is a number by itself on one line and usually looks like 123456-99). I cannot seem to script the next part.

Any help is appreciated. Thank you.
# 2  
Old 07-28-2011
how about this:

Code:
sed '/[0-9]-[0-9][0-9]/{N;/\n$/d}' infile > outfile

# 3  
Old 07-28-2011
Hello Scottie1954 and welcome to the forums.

Perhaps this will do the trick for you:
Code:
sed '/PATTERN/{$q; N; /\n$/d;}'

That will print all lines until it finds pattern. When a line matching PATTERN is found, instead of printing it, the next line is read and appended. We then test to see if the text in the pattern buffer ends with a newline. Since sed never includes the newline from the text that's read from the file, the only way the newline could have gotten there is as a result of the preceding N command. That newline, if at the end of a line, indicates that the last line read was empty. If this condition is met, the d command discards the current pattern buffer (which at this point contains those two lines, one of them empty) and a new cycle begins with the next line in the file.

The $q command is there to immediately print and exit should the last line in the file match PATTERN. While there is no point in checking for a blank line when there are no further lines, the primary reason for this check is that the N command will not output the pattern buffer if there is nothing left to read from the file (if the last line of the file matches PATTERN, without this check it would be lost).

Regards,
Alister

---------- Post updated at 05:45 PM ---------- Previous update was at 05:43 PM ----------

I see that my archenemy, Chubler_XL beat me to it. Smilie

I'll leave mine intact for the explanation and $q difference.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 07-28-2011
Thank you, Chubler_XL and Alister, for replying. I've tried both but hp-ux is having some difficulties.

For Chubler_XL's code, sed tells me:

Code:
sed: Function /[0-9]-[0-9][0-9]/{N;/\n$/d} cannot be parsed.

And for Alister's example:

Code:
sed: /[0-9]-[0-9][0-9]{$q; N; /\n$/d;} is not a recognized function.

I can understand Alister's explanation (that's been the clearest I've found about looking for the newline character), but I'm afraid I don't have enough sed experience to troubleshoot its different flavors among operating systems.

Anyway, if you've got a guess about what's making hp-ux cranky, I'd appreciate it. I'll keep experimenting myself.

~Scottie1954
# 5  
Old 07-28-2011
@Alister - Nice explination and good pickup on pattern on last line of file

Scottie1955 - You could try using awk:

Code:
awk '/[0-9]-[0-9][0-9]/ { K=$0; next }
K { if(length) print K "\n" $0; K=""; next }
1
END { if(K) print K }' infile

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 07-28-2011
Whoa! That worked. Thanks, Chubler_XL.
# 7  
Old 07-28-2011
Quote:
Originally Posted by Scottie1954
Thank you, Chubler_XL and Alister, for replying. I've tried both but hp-ux is having some difficulties.

For Chubler_XL's code, sed tells me:

Code:
sed: Function /[0-9]-[0-9][0-9]/{N;/\n$/d} cannot be parsed.

And for Alister's example:

Code:
sed: /[0-9]-[0-9][0-9]{$q; N; /\n$/d;} is not a recognized function.

Chubler_XL's suggestion is missing a semicolon. To be posix-compliant, there should be a semi-colon after the d command:
Code:
sed '/[0-9]-[0-9][0-9]/{N;/\n$/d;}' infile > outfile

With regard to my example, it seems you may have mistyped it. In your error message, there is no slash delimiting the end of the pattern (what seems to have been omitted is in bold red):
Code:
sed '/PATTERN/{$q; N; /\n$/d;}'

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

Sed find exact string and delete line with variable

All, I am trying to read in a variable and search a file then delete based on that string, but i want to match exact word. This works but it matches all, i don't want to match anthing that contains the string, just the exact string. sed -i "/$feedname/d" file I tried sed... (1 Reply)
Discussion started by: markdjones82
1 Replies

4. Shell Programming and Scripting

sed to find pattern and delete line before and after

I have the following file: line1 line2 MATCH line3 line4 I need to find the pattern, "MATCH" and delete the line before and after MATCH. So the result should be line1 MATCH lline4 I have to use sed because it is the only utility that is common across my environments. I... (1 Reply)
Discussion started by: craftereric
1 Replies

5. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

6. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

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

8. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

9. Shell Programming and Scripting

sed: find match and delete the line above

I am searching a dhcpd.conf to find the hardware ethernet match, then once the match is found delete just the line above it. For example: testmachine.example { hardware ethernet 00:00:00:00:00:00; fixed address 192.168.1.100; next-server 192.168.1.101; filename "linux-install/pxelinux.0"; }... (3 Replies)
Discussion started by: cstovall
3 Replies

10. Shell Programming and Scripting

regex to delete multiple blank lines in a file?

can't figure out a way to delete multiple empty lines but keep single empty lines in a file, file is like this #cat file 1 2 3 4 5 6 - What I want is 1 2 (6 Replies)
Discussion started by: fedora
6 Replies
Login or Register to Ask a Question