Please explain this SED expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please explain this SED expression
# 1  
Old 03-26-2011
Please explain this SED expression

can anyone please explain this code?


Code:
sed ':t /<VirtualHost/,/VirtualHost>/ { /VirtualHost>/!{ $!{ N; bt } }; /name/d; }' infile


Last edited by DukeNuke2; 03-26-2011 at 03:49 AM..
# 2  
Old 03-26-2011
this sed command will remove lines containing "name" within elements <VirtualHost.....> and </VirtualHost.....>.
This User Gave Thanks to sk1418 For This Post:
# 3  
Old 03-26-2011
More like it removes the entry (except the first line) if any line contains "name"...

Code:
$ cat hosttest
stufff
name = name
more stuff
<VirtualHost>
qqqq
qqq2
qqqq3
name=qname
qqqqqq7
qqqqq9
</VirtualHost>
<VirtualHost>
zzzzzz
zzzzzz2
zzzz3
jjje=zzzz
</VirtualHost>
$ sed ':t /<VirtualHost/,/VirtualHost>/ { /VirtualHost>/!{ $!{ N; bt } }; /name/d; }' hosttest
stufff
name = name
more stuff
<VirtualHost>
<VirtualHost>
zzzzzz
zzzzzz2
zzzz3
jjje=zzzz
</VirtualHost>
 $

# 4  
Old 03-26-2011
there is a remaining
<VirtualHost>
which makes the output ugly

---------- Post updated at 10:40 PM ---------- Previous update was at 10:39 PM ----------

(the one just after "more stuff") Smilie
# 5  
Old 03-27-2011
That unwanted "<VirtualHost>" is there because the /VirtualHost>/ pattern matches both the opening and closing markup. That causes { /VirtualHost>/!{ $!{ N; bt } } to not be executed for the first line of the block, hence that first "<VirtualHost>" will always be emitted by the implicit print at the end of the sed script since -n is not used. This also means that there's a logic error: /name/d will attempt to find a match after the first line of each block in addition to after the full block has been accumulated (which is what is intended).

To fix this problem, tighten up the regular expressions. The following should be better (I am not writing it on one line since my sed does not support the gnu extensions which allow labels to be followed by anything other than a newline):
Code:
:t
/<VirtualHost>/,/<\/VirtualHost>/ {
    /<\/VirtualHost>/! {
        $! {
            N
            b t
        }
    }
    /name/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

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

2. Shell Programming and Scripting

Explain sed command

sed -e 's,*$,,' Can someone explain the options in this command? (2 Replies)
Discussion started by: scj2012
2 Replies

3. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

4. UNIX for Dummies Questions & Answers

Can someone please explain sed -n '/^$/!{s/<[^>]*>//g;p;}' filename

I came across this sed expression, and it does exactly what I want. However I haven't got the faintest clue how it does it and thus do not feel capable of using it. Can someone please explain how this expression works? (I used it to remove html tags in a html file I was converting to text) ... (3 Replies)
Discussion started by: maximus73
3 Replies

5. Shell Programming and Scripting

can you explain this sed code?

can anyone please explain this code? sed ':a;N;$!ba;s/]\n//g' file it replaces lines ending with "]" and concatenates with the next line so that line1] line2 becomes line1line2 i don't understand this part: :a;N;$!ba; I have noted that I can replace "a" with any letter: ... (1 Reply)
Discussion started by: locoroco
1 Replies

6. UNIX for Advanced & Expert Users

Please explain this sed one liner

Can anyone explain the below sed oneliner? sed -e ':a' -e '$q;N;11,$D;ba' It works same as tail command. I just want to know how it works. Thanks ---------- Post updated at 11:42 PM ---------- Previous update was at 11:37 PM ---------- Moderators, Can you please delete this thread?... (0 Replies)
Discussion started by: pandeesh
0 Replies

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

8. Shell Programming and Scripting

sed -e 's%/$%%' explain this command

mount -ps | tail -1 | awk '{print $1}' | sed -e 's%/$%%' can you please explain this command mainly sed -e 's%/$%%' this.... (5 Replies)
Discussion started by: rsivasan
5 Replies

9. Shell Programming and Scripting

Explain SED code

Hi, Can anyone pls explain me the below SED code in detail. sed -e :a -e '$!N;s/\n//;ta' -e P -e D When this code is executed with a file(has 1lac records), it is taking very long time to process. So I wanted to modify this SED code with equivalant AWK code. Thanks, Sri (1 Reply)
Discussion started by: srilaxmi
1 Replies

10. Shell Programming and Scripting

please explain this sed shell script to remove C++ comments.

#! /bin/sed -nf # Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com) # Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org) # Works its way through the line, copying to hold space the text up to the # first special character (/, ", '). The original... (1 Reply)
Discussion started by: Priyaranjan
1 Replies
Login or Register to Ask a Question