Another sed Syntax Puzzle . . .


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Another sed Syntax Puzzle . . .
# 1  
Old 09-25-2014
Another sed Syntax Puzzle . . .

Greetings!

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

Consider the following file content to be worked through:
Quote:
Something: Objects
This is the content

Something: Object
This is the content

Something: Objection
This is the content
What needs to happen is the
Quote:
Something: Object
This is the content
block should be removed up to and including the following blank line, leaving
Quote:
Something: Objects
This is the content

Something: Objection
This is the content
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!
# 2  
Old 09-25-2014
Different Objects or just one? I am still trying to find the requirement.
# 3  
Old 09-25-2014
I have a file "test2.sed" with this content
Code:

 /Objects/ {
 N
 N
 d
 }
 

and the command would be
Code:
sed -f test2.sed filename

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

HTH
# 4  
Old 09-25-2014
As long as there is a blank line following the segment you want to delete, the following should work:
Code:
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-25-2014
Perhaps
Code:
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.
# 6  
Old 09-25-2014
Thanks so much, folks for the help!

@Don Cragun:
Quote:
sed '/^Something: Object[[:space:]]*$/,/^[[:space:]]*$/d' file
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 Smilie

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!
# 7  
Old 09-25-2014
Quote:
Originally Posted by LinQ
... ... ...

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

... ... ...
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. Smilie 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.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Can sed use a file on the syntax?

Dear all, I need help, again. I would like to use a sed on a for. Is is possible to ask sed to call a file in the syntax? For exemple: sed "/Y/ s/number/X/" test_imput > test_output where Y is a file which inside there is one pattern only; Also X is a file with one... (6 Replies)
Discussion started by: lColli
6 Replies

2. Shell Programming and Scripting

sed syntax error

Hi, In the following excerpt of shell script code: I could not understand the sed syntax. Could anyone shed some light on this? configure_ssl() { jboss_conf_file=$1 echo "Configuring SSL for -" ${jboss_conf_file} isSSLSetup=`echo cat ${jboss_conf_file} | grep <Connector... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

sed s/// syntax help

<tr><td width=10% style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>Package</b></td><td width=10% valign=center style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>JTs</b></td> This is got to be simple. I run this on the above .html file: sed... (8 Replies)
Discussion started by: dba_frog
8 Replies

4. UNIX for Dummies Questions & Answers

sed - need help for syntax

Hello, here is what I've got : FILE='/OPERATIONNEL/SATURNE/CHAMPS/MASTER/ANA/SATURNE_1DAV_20080805_20080806_S3D_T_R20080806.NC ';;... (4 Replies)
Discussion started by: Aswex
4 Replies

5. Shell Programming and Scripting

sed - multi-line regex syntax eval puzzle

sed novice bashing away at this.... I am trying to build a sed script that will find the instances of "cn" that have more than one "DirXML" value on them.... see sample below: I am not having any luck with any variation that tries to find "DirXML.*\nDirXML.*". Isn't there a way to get sed to... (6 Replies)
Discussion started by: gosleddog
6 Replies

6. Shell Programming and Scripting

Explain following sed syntax please

Thanks to this forum I have managed to work out a solution to my problem and actually understand most of it, but one thing is confusing me and I am sure someone here can explain. I need to insert a piece of txt into a file. This txt is awk '{ sub(/$/,"\r"); print }' $JCL_WBB50103_EFTOUT >... (2 Replies)
Discussion started by: hukcjv
2 Replies

7. Shell Programming and Scripting

What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of sed 3,3,c\replacement stuff\ filename.txt with no success. about the only thing that causes any reaction is sed 3,3c\\ filename.txt but it just prints out the whole file. ... (13 Replies)
Discussion started by: SusanDAC
13 Replies

8. Shell Programming and Scripting

syntax for variables in sed

I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have: sed 's/$name/newname/g'... (15 Replies)
Discussion started by: eltinator
15 Replies

9. Shell Programming and Scripting

Help for Sed Syntax problem

I have one File named "txt_file" # cat txt_file <DBType>RT</DBType> <AppType>RT</AppType> -------------------------------------------------- I want replace "<AppType>RT</AppType>" to <AppType>XY</AppType> in txt_file and output redirect to Newfile ... (2 Replies)
Discussion started by: SanjayLinux
2 Replies

10. UNIX for Dummies Questions & Answers

sed syntax

Hi, How can i use sed command to modify a part of a variable containing "/" by another containing "/" like describe below: VAR="/app/share/eai" VAR1="/app/share" VAR2="/data/test" echo $VAR | sed 's/... ??? # using sed to replace $VAR1 in $VAR by $VAR2 ? (4 Replies)
Discussion started by: jo_aze
4 Replies
Login or Register to Ask a Question