Another sed Syntax Puzzle . . .

Greetings!

Have a quick question for the community today; this time looking at a nifty little sed puzzle :wink:

Consider the following file content to be worked through:

What needs to happen is the

block should be removed up to and including the following blank line, leaving

I have bits and pieces of how this could work, but nothing coherent (usable).

Any ideas as to how one might form a sed -i commandline to bash this out?

Also, any favorite sed resources/examples out there to recommend???

Thanks!

Different Objects or just one? I am still trying to find the requirement.

I have a file "test2.sed" with this content

 /Objects/ {
 N
 N
 d
 }
 

and the command would be

sed -f test2.sed filename

. With only three examples it wouldn't work with more than one "content" line.

HTH

As long as there is a blank line following the segment you want to delete, the following should work:

sed '/^Something: Object[[:space:]]*$/,/^[[:space:]]*$/d' file

but this won't work if there is no blank line after the segment you want to remove (such as if it is the last segment in the file). If you also want to be able to delete that text if is is the last segment in the file and it doesn't have a blank line following it in that case, I find awk easier to use:

awk '
/^Something: Object[[:space:]]*$/ { del = 1 }
!del
/^[[:space:]]*$/ {del = 0}
' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

1 Like

Perhaps

sed -i '/^Something: Object$/,+2d' file

---------- Post updated at 09:42 PM ---------- Previous update was at 09:40 PM ----------

Note: tailored for the provided sample data.

Thanks so much, folks for the help!

@Don Cragun:

is conceptually what I had in mind; but didn't have a way of cobbling it all together. I'll take it out for a spin in just a shake :wink:

Yes, there will always be a blank line following the block to remove; so the end marker is valid. I just checked the EOF, and there are a couple of empty lines there, too; so all should be OK...

Apart from enormous skill, what does one look to for this type of syntax? It would be immensely helpful if there was an applet on a site somewhere to pop out the commands; but I guess the world isn't finished yet...

Thanks again, everyone, for the help!

---------- Post updated at 04:16 PM ---------- Previous update was at 03:45 PM ----------

Yep, worked like a charm.

The only modification I added was the -i after sed --

Cheers!


  1. [:space:] ↩ī¸Ž

If you are serious about learning how to use Linux and UNIX system utilities, first carefully study the differences between filename pattern matching, basic regular expressions, and extended regular expressions and learn how to write patterns, BREs, and EREs to match whatever you might want to match. (Then you'll immediately know what type of expression you need to use for a particular type of string match.) Then study the man pages for the utilities you want to use. Everything you needed for this job comes from knowing BREs and from the address ranges section of the sed man page.

And, finally, practice makes perfect. Play around with various problems until the solutions to them just flow from your fingertips. :smiley: Read the problems posed here; look at the suggestions provided; if they don't make sense to you, read the man pages; if they still don't make sense, ask questions!

Learn something new everyday.

1 Like

Indeed so. And thanks for the information.

One last question: [[:space:]] is not quite clear to me. Why not the plain regex [:space:] ?

(Can't find where this issue is addressed.)

:slight_smile:

The BRE and ERE [:space:] is a bracket expression that will match any one of the characters "a", "c", "e", "p", "s", and ":". While [[:space:]] is a bracket expression that will match any character in the space character class. (In the C and POSIX locales, the space character class includes the <space>, <form-feed>, <newline>, <carriagereturn>, <tab>, and <vertical-tab> characters.)

And BRE and ERE [^[:lower:][:digit:]] is a non-matching bracket expression that will match any character that is not a lowercase alphabetic character and is not a decimal digit in the current locale.

1 Like