1834144 Members
2054 Online
110064 Solutions
New Discussion

regular expression

 
SOLVED
Go to solution
Andreas Voss
Honored Contributor

regular expression

Hi,

i'm confused with an RE i've found in a script:

sed '/^{\\{/,/^}\\}/d'

Has anybody the expierience to explain me what this sed does ?

Thanks
12 REPLIES 12
Bill McNAMARA_1
Honored Contributor

Re: regular expression

does it work!!!!!?
It works for me (tm)
Frederic Soriano
Honored Contributor

Re: regular expression

Andreas,

I recognize this one-liner is a bit complex: it prints all of input file (or echoed piped input) EXCEPT section between 2 regexp, knowing that regexp one is:

* line beginning with opening curly brace, followed by a backslash and immediately followed by another opening curly brace

and regexp 2 is:
* line beginning with closing curly brace, followed by a backslash and immediately followed by another closing curly brace.

So if your file looks like this:
--snip--
this is line 0
{\{ this is line 1
line 2
...
line n
}\} this is a trailling text
--snip--

then 'sed' output will give:

--snip--
this is line 1
line 2
...
line n
--snip--

I hope this helps !

Best regards,

Fred.
Jason VanDerMark
Trusted Contributor

Re: regular expression

Well, at first glance I know that it deletes something. This is denoted by the / at the beginning and the /d at the end. The middle is what to delete, a set of criteria. I will do some research on the criteria and reply again.
Tie two birds together, eventhough they have four wings, they cannot fly.
Rita C Workman
Honored Contributor

Re: regular expression

What a "&*(&" Brain Drain....
I tried it...it will parse. But I can't figure out what it's supposed to do...it seems to violate some sed tructure even though it parses...I mean if you want to match in sed you'd \{ \} So I can't figure the second set of { } surrounding it because { } is awk not sed.
And that double \\ am I missing something--one \ means turns off the special meaning of the next character, which would negate the \{ \} sed match. The ^ says match the following at the beginning of the line, so what's that other ^ doing.
Oh my head.....I tried creating a file with some bogus lines starting with a variety of { / \ and so on...
You know what I got....
The statement parses ok...and produces a file with all my lines still in tact.
I'm going to find some excedrin now...I hope someone else had better results...

/rcw
James R. Ferguson
Acclaimed Contributor

Re: regular expression

Hi Andreas!

...so was the double backslash a carry-over for those of us who remember the now-defunct need to escape the backslash with itself in order to post to this forum???...so it would read (?):

# sed '/^{\{/,/^}\}/d'

with warm regards!

...JRF...
Patrick Wallek
Honored Contributor

Re: regular expression

Based Frederico's reply, I tried an experiment:

I used this testfile:

{\{dir
/dir/dir2/dir4
}\} /
/dir/dir2/dir5
/dir/dir2/dir6
{\{


And cat'ed it to that sed command:

# cat file | sed '/^{\\{/,/^}\\}/d'
/dir/dir2/dir5
/dir/dir2/dir6

James R. Ferguson
Acclaimed Contributor

Re: regular expression

Hello again:

Nope. Ugly. Double backslashes are correct. It won't parse otherwise. Should have tried it first ;-)

Great presentation of explanation, Fred!!!

Regards to all!

...JRF...
Frederic Soriano
Honored Contributor
Solution

Re: regular expression

Yup Rita, I completely agree! This one is a source of headaches!

To shed some light on this, let's take the following command:

sed '/Iowa/,/Montana/d'

Two regexp are denoted, which are strings "Iowa" and "Montana". sed finds these two strings and uses them as start and end delimiters (see the ending d command), printing whatever is between both (uh I hope you understand -- I'm afraid my english is not as good as I'd like it to be ;o))

--

Curly braces, when backslashed, match a range of occurrences of the single char that immediately precedes it. In Andreas' case, they are not backslashed but considered as curly braces.

--

Finally, you are right when saying that a single \ escapes the special char that follows, but if you want to match the char "backslash" (\), then you have to backslash it once more !

I attached to this post a file that contains many sed one-liners, including this one. I'm sure you'll find it useful.

HTH !

Fred.
Frederic Soriano
Honored Contributor

Re: regular expression

Patrick,

Yes you're right. I even said it: "this sed command prints all of file EXCEPT section between two regexps", and i confused myself when replying:

--snip--
this is line 1
this is line 2
...
this is line n
--snip--

Of course, this is the opposit, ie:

--snip--
this is line 0
this is the trailing text
--snip--

Again, sorry for mistake! (Uh where is my AlkaSeltzer ?!)

Fred.
Andreas Voss
Honored Contributor

Re: regular expression

Hi all,

I would have never thought that this question would release as much.

Thank you for all the great responses.

Kind Regards

Andreas
Rita C Workman
Honored Contributor

Re: regular expression

Frederic,

Many Thanks - and you explained it well !

I am working at trying to improve my very basic sed & awk (much needed) skills...so things like this really help make me think.
And Thanks to Andreas for posting the challenge !

/rcw



Patrick Wallek
Honored Contributor

Re: regular expression

Rita,

O'Reilly puts out what I have heard is a very good book on sed and awk. It is called 'SED & AWK' and it is one of the nutshell books that they put out. It is recommended to get at least the 2nd edition.

When I was at HP World in San Francisco in 1999, I attended a very good seminar on awk that David Totsch tought. I have a hard copy of the seminar handout, and it should be on the HP World CD, if I can find it. If you are interested let me know. (patrickwallek@upr.com)