Help understanding a sed expression.

458 views
Skip to first unread message

peteris...@gmail.com

unread,
Aug 8, 2006, 5:25:58 PM8/8/06
to
Hello

I was going through USEFUL ONE-LINE SCRIPTS FOR SED by Eric Pement and
noticed this interesting sed expression which I am having trouble
deciphering:

sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'

Formatting it for better visual:
sed '
/\n/ !G
s/\(.\)\(.*\n\)/&\2\1/
//D
s/.//
'

What it does is reverse lines just like `rev' program.

Could anyone comment how this particular expression works?

At the moment I don't understand in which cases the /\n/ matches
(in both //D and /\n/ !G commands).
For example a test program: echo test | sed -n '/\n/ p' never prints
anything.


Thanks!


P.Krumins

Xicheng Jia

unread,
Aug 8, 2006, 6:55:53 PM8/8/06
to
peteris...@gmail.com wrote:
> Hello
>
> I was going through USEFUL ONE-LINE SCRIPTS FOR SED by Eric Pement and
> noticed this interesting sed expression which I am having trouble
> deciphering:
>
> sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
>
> Formatting it for better visual:
> sed '
> /\n/ !G
> s/\(.\)\(.*\n\)/&\2\1/
> //D
> s/.//
> '

This is a very interesting sed command line. :-)

/\n/!G
this line is trival, it means if there is no newline in your input
stream, then you append one at the end.


s/\(.\)\(.*\n\)/&\2\1/;

a simple s/// expression where you group the first character as \1, all
others as \2, then replace these whole matched stuff with "&\2\1",
where & is the whole matched text or "\1\2".. so if the input string is
"1234" then after s/// expression, it becomes "1234\n234\n1".

//D;
this statement is the key, since empty pattern matches the last
existing regex, so it's exactly the same as: /\(.\)\(.*\n\)/D

D command means(From my pocket reference): Delete from the start of the
input till the first embedded newline and then *resume* editing with
first command in script. so you got a loop: as long as
/\(.\)\(.*\n\)/ satisfied, sed will resume all previous operations to
handle the text in the pattern space..

After several loop, the text in the pattern space becomes "\n4321",
when /\(.\)\(.*\n\)/ fails, and then the sed goes to the next command.

s/.// : remove the first character in the pattern space which is the
newline..

So, after you understand what "//D" does, then you know how your script
works.

Hope this helps.
Xicheng

peteris...@gmail.com

unread,
Aug 9, 2006, 4:03:22 AM8/9/06
to

Xicheng Jia wrote:
>
> So, after you understand what "//D" does, then you know how your script
> works.
>
> Hope this helps.
> Xicheng


Thanks, now I fully understand this expression :)


P.Krumins

Message has been deleted

Lew Pitcher

unread,
Aug 26, 2020, 4:04:18 PM8/26/20
to
On August 26, 2020 15:50, Cassie Newman wrote:

> How would you apply this

? what ?

> to only 1 particular line in a file?

--
Lew Pitcher
"In Skills, We Trust"

Keith Thompson

unread,
Aug 26, 2020, 8:28:14 PM8/26/20
to
Cassie Newman <csne...@ncsu.edu> writes:
> How would you apply this to only 1 particular line in a file?

I see you posted this via Google Groups.

The parent article doesn't appear to exist on my server -- which is one
reason we strongly recommend quoting the relevant parts of any article
you reply to.

Jumping over to the Google Groups web page, I see you replied to an
article from 14 years ago.

https://groups.google.com/g/comp.unix.shell/c/s4hfVpJYX2Q/m/ZySILlWwCQAJ

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Kaz Kylheku

unread,
Aug 26, 2020, 8:44:25 PM8/26/20
to
On 2020-08-26, Cassie Newman <csne...@ncsu.edu> wrote:
> How would you apply this to only 1 particular line in a file?

You replied to a fourteen-year-old Usenet article that you found in
Google's archive, without quoting any of it.

Nobody has this article in their local NNTP server any more.

I can see that the Message-ID

<1155072358....@h48g2000cwc.googlegroups.com>

is given in your References: header, but it has been yeras and years
since Google Groups had an advanced search where you could paste a
such a Message-ID to retrieve an article.

However, the "Help understanding a sed expression" subject line is quite
unique and using that, we can search Googles archive to trace it to a
discussion started here on August 8, 2006 by this article:

https://groups.google.com/forum/message/raw?msg=comp.unix.shell/s4hfVpJYX2Q/w_4N2hXwQxsJ

which matches one given in your References: line.

Anyway, how you apply any Sed expression to just one line is by naming
that line as an address.

E.g. edit "this" to "that" in line 42:

sed -e '42s/this/that'

42 is the address specifying line 42, s is the substitution command, and
so forth.

Lines other than 42 are passed through unedited.

There are other ways to specify the line, if it doesn't have a fixed
number. However, it can get tricky if you specify a line using a
condition which matches multiple lines, and you only want it to match
one of those lines.

For that, the easiest thing is may be to use Awk.

Given that Awk exists, and has been a staple part of Unix for almost
forty years, and is part of POSIX and all, it's not worth knowing much
more about sed than "sed -e s/this/that/".

Javier

unread,
Aug 27, 2020, 9:13:27 AM8/27/20
to
Kaz Kylheku <793-84...@kylheku.com> wrote:
> You replied to a fourteen-year-old Usenet article that you found in
> Google's archive, without quoting any of it.
>
> Nobody has this article in their local NNTP server any more.

My ISP newsserver does have it. I think they rely on giganews for the
news server. They keep all the articles since 2003 (more than 100k
for this group).

> I can see that the Message-ID
>
> <1155072358....@h48g2000cwc.googlegroups.com>

In my ISP this shows the original message from 2006.

lynx nntp://usenet.plus.net/1155072358....@h48g2000cwc.googlegroups.com

Eric

unread,
Aug 27, 2020, 11:10:06 AM8/27/20
to
On 2020-08-27, Javier <jav...@invalid.invalid> wrote:
> Kaz Kylheku <793-84...@kylheku.com> wrote:
>> You replied to a fourteen-year-old Usenet article that you found in
>> Google's archive, without quoting any of it.
>>
>> Nobody has this article in their local NNTP server any more.
>
> My ISP newsserver does have it. I think they rely on giganews for the
> news server. They keep all the articles since 2003 (more than 100k
> for this group).

There may be others, either in their own right or using something like
giganews, but who knows where they are, except for those that already
use them.
>
>> I can see that the Message-ID
>>
>> <1155072358....@h48g2000cwc.googlegroups.com>
>
> In my ISP this shows the original message from 2006.
>
> lynx nntp://usenet.plus.net/1155072358....@h48g2000cwc.googlegroups.com

Which doesn't work for anyone without a plusnet login! Most, if not all,
of the other possible sources will also need accounts.

Eric
--
ms fnd in a lbry
Reply all
Reply to author
Forward
0 new messages