Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The last blank line can be removed by sed.

45 views
Skip to first unread message

hongy...@gmail.com

unread,
Jan 10, 2023, 3:32:05 AM1/10/23
to
See my following 2 testings:

Testing 1:

werner@X10DAi:~$ echo '?*' | gp -fq | grep -v 'type RETURN' | sed -Ee 's/[ ]+/\n/g;/^$/d' | tail | cat -A
znconreychar$
znconreyconductor$
znconreyexp$
znconreylog$
zncoppersmith$
znlog$
znorder$
znprimroot$
znstar$
$
werner@X10DAi:~$

Testing 2:
werner@X10DAi:~$ echo '?*' | gp -fq | grep -v 'type RETURN' | sed -Ee 's/[ ]+/\n/g' | sed -Ee '/^$/d' | tail | cat -A
znchartoprimitive$
znconreychar$
znconreyconductor$
znconreyexp$
znconreylog$
zncoppersmith$
znlog$
znorder$
znprimroot$
znstar$
werner@X10DAi:~$

As you can see, if I combine the above two sed commands into a single sed
call, the last line will be blank in the output, as shown in the testing 1.

Is there any explanation for this behavior?

Regards,
Zhao

Janis Papanagnou

unread,
Jan 10, 2023, 7:38:49 AM1/10/23
to
$ gp
ksh: gp: not found [No such file or directory]

(Why don't you provide a minimum working example as you've been
suggested many times already?)

>
> Is there any explanation for this behavior?

$ { seq 3 ; echo ;}
1
2
3

$ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g;/^$/d'
1
2
3
$ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g' | sed -Ee '/^$/d'
1
2
3

Works for me. (Same output.)

BTW, what is -E supposed to do? (Use of EREs instead of BREs?)
Option -E is not documented in my sed man page.

(You may want to consider using awk instead of these grep/sed/...
pipelines. As a side-effect that may also fix your issue.)

Janis

>
> Regards,
> Zhao
>

Geoff Clare

unread,
Jan 10, 2023, 8:41:10 AM1/10/23
to
Janis Papanagnou wrote:

> On 10.01.2023 09:32, hongy...@gmail.com wrote:
>>
>> As you can see, if I combine the above two sed commands into a single sed
>> call, the last line will be blank in the output, as shown in the testing 1.
>>
>> Is there any explanation for this behavior?

REs are matched against the "pattern space", not individual lines
within the pattern space. Normally the pattern space only contains
one line, but the substitution with a \n on the right hand side makes
it contain multiple lines. Try this:

sed -Ee 's/[ ]+/\n/g;s/\n$//'

> $ { seq 3 ; echo ;}
> 1
> 2
> 3
>
> $ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g;/^$/d'
> 1
> 2
> 3
> $ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g' | sed -Ee '/^$/d'
> 1
> 2
> 3
>
> Works for me. (Same output.)

That's because the input to sed didn't include any space characters.
Try it with { seq 3 ; echo ' ' ;} | ...

> BTW, what is -E supposed to do? (Use of EREs instead of BREs?)

You guessed right. It will be in the next version of POSIX.

--
Geoff Clare <net...@gclare.org.uk>

hongy...@gmail.com

unread,
Jan 10, 2023, 9:23:39 AM1/10/23
to
gp is an easy-to-use interactive shell giving access to the PARI functions:

https://pari.math.u-bordeaux.fr/

> (Why don't you provide a minimum working example as you've been
> suggested many times already?)

I'm in a hurry and just think this is an open source software package, which is available in package repositories of various *nix systems.

> >
> > Is there any explanation for this behavior?
> $ { seq 3 ; echo ;}
> 1
> 2
> 3
>
> $ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g;/^$/d'
> 1
> 2
> 3
> $ { seq 3 ; echo ;} | sed -Ee 's/[ ]+/\n/g' | sed -Ee '/^$/d'
> 1
> 2
> 3
>
> Works for me. (Same output.)

I also confirmed your statement with your above examples.

> BTW, what is -E supposed to do? (Use of EREs instead of BREs?)
> Option -E is not documented in my sed man page.

werner@X10DAi:~$ sed --version |head -2
sed (GNU sed) 4.8
Packaged by Debian
werner@X10DAi:~$ sed --help|grep -- -E
-E, -r, --regexp-extended
(for portability use POSIX -E).

hongy...@gmail.com

unread,
Jan 10, 2023, 9:27:41 AM1/10/23
to
Yes. As shown below:

werner@X10DAi:~$ { seq 3 ; echo ' ';} | sed -Ee 's/[ ]+/\n/g;/^$/d' | cat -A
1$
2$
3$
$
$
werner@X10DAi:~$ { seq 3 ; echo -n ' ';} | sed -Ee 's/[ ]+/\n/g;/^$/d' | cat -A
1$
2$
3$
$
werner@X10DAi:~$

hongy...@gmail.com

unread,
Jan 11, 2023, 3:41:05 AM1/11/23
to
P.S. Based on the discussion here [1], the reason for the problem here is explained in the info of sed, but not in man:

werner@X10DAi:~$ sed --version |head -3
sed (GNU sed) 4.8
Packaged by Debian
Copyright (C) 2020 Free Software Foundation, Inc.

werner@X10DAi:~$ info sed |grep -A5 -i 'In a substitution command'
In a substitution command, the 'w' flag writes the substitution
result to a file, and the 'e' flag executes the subsitution result
as a shell command. As with the 'r/R/w/W/e' commands, these must
be terminated with a newline. If whitespace, comments or
semicolons are found, they will be included in the shell command or
filename, leading to unexpected results:

werner@X10DAi:~$ man sed |grep -i 'In a substitution command'

werner@X10DAi:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.10
Release: 22.10
Codename: kinetic

[1] http://pari.math.u-bordeaux.fr/archives/pari-users-2301/msg00049.html

Best,
Zhao

Spiros Bousbouras

unread,
Jan 11, 2023, 9:19:31 AM1/11/23
to
On Wed, 11 Jan 2023 00:41:02 -0800 (PST)
"hongy...@gmail.com" <hongy...@gmail.com> wrote:
> On Tuesday, January 10, 2023 at 10:27:41 PM UTC+8, hongy...@gmail.com wrote:
> > On Tuesday, January 10, 2023 at 9:41:10 PM UTC+8, Geoff Clare wrote:
> > > Janis Papanagnou wrote:
> > >
> > > > On 10.01.2023 09:32, hongy...@gmail.com wrote:
> > > >>
> > > >> As you can see, if I combine the above two sed commands into a single sed
> > > >> call, the last line will be blank in the output, as shown in the testing 1.
> > > >>
> > > >> Is there any explanation for this behavior?
> > > REs are matched against the "pattern space", not individual lines
> > > within the pattern space. Normally the pattern space only contains
> > > one line, but the substitution with a \n on the right hand side makes
> > > it contain multiple lines. Try this:
> > >
> > > sed -Ee 's/[ ]+/\n/g;s/\n$//'

^^^^^^^^^^^^^^^^^^^^^^^^^^
Solution given.


> P.S. Based on the discussion here [1], the reason for the problem here
> is explained in the info of sed, but not in man:
>
> werner@X10DAi:~$ sed --version |head -3
> sed (GNU sed) 4.8
> Packaged by Debian
> Copyright (C) 2020 Free Software Foundation, Inc.
>
> werner@X10DAi:~$ info sed |grep -A5 -i 'In a substitution command'
> In a substitution command, the 'w' flag writes the substitution
> result to a file, and the 'e' flag executes the subsitution result
> as a shell command. As with the 'r/R/w/W/e' commands, these must
> be terminated with a newline. If whitespace, comments or
> semicolons are found, they will be included in the shell command or
> filename, leading to unexpected results:

No , this quote is unrelated. The explanation for the beaviour you observed
(I wouldn't call it a problem) is explained by Geoff above and he gives a
solution too.

> [1] http://pari.math.u-bordeaux.fr/archives/pari-users-2301/msg00049.html

--
Forth is a strange combination of computer language and religion.
Sol Guber at http://mirror.optus.com.au/pub/forth/Archive/docs/mforth.rev

hongy...@gmail.com

unread,
Jan 11, 2023, 9:56:05 AM1/11/23
to
Who is Geoff?

Kenny McCormack

unread,
Jan 11, 2023, 10:19:04 AM1/11/23
to
In article <2516ca7d-4c85-4af5...@googlegroups.com>,
hongy...@gmail.com <hongy...@gmail.com> wrote:
...
>Who is Geoff?

Who is John Galt?

--

First of all, I do not appreciate your playing stupid here at all.

- Thomas 'PointedEars' Lahn -

Spiros Bousbouras

unread,
Jan 11, 2023, 10:33:55 AM1/11/23
to
On Wed, 11 Jan 2023 06:55:51 -0800 (PST)
"hongy...@gmail.com" <hongy...@gmail.com> wrote:
> Who is Geoff?

Who knows ? So many names , so many people , it's hard to keep track of
all of them.

Janis Papanagnou

unread,
Jan 11, 2023, 11:04:20 AM1/11/23
to
On 11.01.2023 15:55, hongy...@gmail.com wrote:
>
> Who is Geoff?

Not a person, just an acronym; General Expert Of Fabulous Facts.

Janis

hongy...@gmail.com

unread,
Jan 11, 2023, 9:01:37 PM1/11/23
to
I have since examined the previous discussions of this thread and feel that the name Geoff, in the context of this topic, must refer to "Geoff Clare", who had posted nice comments on the question here: https://groups.google.com/g/comp.unix.shell/c/xOUgJUut6b8/m/EFQy8LKECAAJ.

> Janis
Zhao

Geoff Clare

unread,
Jan 12, 2023, 8:41:08 AM1/12/23
to
Thanks for that Janis. Can't remember the last time a Usenet post
made me laugh out loud.

--
Geoff Clare <net...@gclare.org.uk>

Ed Morton

unread,
Jan 13, 2023, 10:05:20 AM1/13/23
to
On 1/10/2023 8:23 AM, hongy...@gmail.com wrote:
> On Tuesday, January 10, 2023 at 8:38:49 PM UTC+8, Janis Papanagnou wrote:
>> On 10.01.2023 09:32, hongy...@gmail.com wrote:
>>> See my following 2 testings:
>>>
>>> Testing 1:
>>>
>>> werner@X10DAi:~$ echo '?*' | gp -fq | grep -v 'type RETURN' | sed -Ee 's/[ ]+/\n/g;/^$/d' | tail | cat -A
<snip>
>> $ gp
>> ksh: gp: not found [No such file or directory]
>
> gp is an easy-to-use interactive shell giving access to the PARI functions:
>
> https://pari.math.u-bordeaux.fr/
>
>> (Why don't you provide a minimum working example as you've been
>> suggested many times already?)
>
> I'm in a hurry

YMMV putting your time constraints ahead of the time constraints of
everyone else who might be willing to try to help you. But, somehow you
still get answers so I guess you're absolutely right - there's no reason
for you to try to make it easy for people to help you, just do whatever
is quickest and easiest for you.

Ed.

hongy...@gmail.com

unread,
Jan 13, 2023, 10:41:39 PM1/13/23
to
Everything in the world has its own reason. The difficulties and ease in form certainly have a corresponding impact. However, they are relative, after all.

The Internet is so huge, and the *nix and open source world are so rich and colorful. You never know what you find difficult may just be the easiest and favorite thing in another person's eyes.

> Ed.
Zhao

Janis Papanagnou

unread,
Jan 14, 2023, 5:42:05 AM1/14/23
to
On 14.01.2023 04:41, hongy...@gmail.com wrote:
> On Friday, January 13, 2023 at 11:05:20 PM UTC+8, Ed Morton wrote:
>>>
>>>> (Why don't you provide a minimum working example as you've been
>>>> suggested many times already?)
>>>
>>> I'm in a hurry
>> YMMV putting your time constraints ahead of the time constraints of
>> everyone else who might be willing to try to help you. But,
>> somehow you still get answers so I guess you're absolutely right -
>> there's no reason for you to try to make it easy for people to help
>> you, just do whatever is quickest and easiest for you.
>
[...]
>
> The Internet is so huge, and the *nix and open source world are so
> rich and colorful. You never know what you find difficult may just be
> the easiest and favorite thing in another person's eyes.

You seem to be basically talking about the topics (the "difficulties")
while Ed was talking about mindset and personal posting habit.

>
>> Ed.
> Zhao
>

Kenny McCormack

unread,
Jan 14, 2023, 7:17:13 AM1/14/23
to
In article <tpu0to$1uqpk$1...@dont-email.me>,
Janis Papanagnou <janis_pap...@hotmail.com> wrote:
...
>You seem to be basically talking about the topics (the "difficulties")
>while Ed was talking about mindset and personal posting habit.

What does that mean?

--
The motto of the GOP "base": You can't *be* a billionaire, but at least you
can vote like one.
0 new messages