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:
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.
Perhaps this will do the trick for you:
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.
I'll leave mine intact for the explanation and $q difference.
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:
And for Alister's example:
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.
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:
And for Alister's example:
Chubler_XL's suggestion is missing a semicolon. To be posix-compliant, there should be a semi-colon after the d command:
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):
Regards,
Alister
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)