Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #226370 > unrolled thread

POSIX compliant sed making newline character

Started byjohn doe <johndoe65534@mail.com>
First post2020-08-21 19:50 +0200
Last post2020-08-21 21:10 +0200
Articles 10 — 3 participants

Back to article view | Back to linux.debian.user


Contents

  POSIX compliant sed making newline character john doe <johndoe65534@mail.com> - 2020-08-21 19:50 +0200
    Re: POSIX compliant sed making newline character Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-21 20:00 +0200
      Re: POSIX compliant sed making newline character john doe <johndoe65534@mail.com> - 2020-08-21 20:10 +0200
        Re: POSIX compliant sed making newline character Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-21 20:40 +0200
          Re: POSIX compliant sed making newline character Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-21 20:40 +0200
            Re: POSIX compliant sed making newline character john doe <johndoe65534@mail.com> - 2020-08-21 21:00 +0200
              Re: POSIX compliant sed making newline character john doe <johndoe65534@mail.com> - 2020-08-21 21:10 +0200
                Re: POSIX compliant sed making newline character Andrei POPESCU <andreimpopescu@gmail.com> - 2020-08-22 18:40 +0200
                  Re: POSIX compliant sed making newline character john doe <johndoe65534@mail.com> - 2020-08-22 20:50 +0200
              Re: POSIX compliant sed making newline character Greg Wooledge <wooledg@eeg.ccf.org> - 2020-08-21 21:10 +0200

#226370 — POSIX compliant sed making newline character

Fromjohn doe <johndoe65534@mail.com>
Date2020-08-21 19:50 +0200
SubjectPOSIX compliant sed making newline character
Message-ID<AGjkR-7v0-1@gated-at.bofh.it>
Hello all,

I'm trying to use the command provided at (1):

$ sed '/pattern{N;N;a try\d10}' input-file
sed: -e expression #1, char 0: unmatched `{'

As I understanded, I should make a newline before the right brace.
I thought that '\d10' should do it but as shown above it does not.

What am I missing?


The idea is to be able to append a line two lines after a match while
being POSIX compliant.

1)
https://stackoverflow.com/questions/30099736/sed-insert-line-after-x-lines-after-match

--
John Doe

[toc] | [next] | [standalone]


#226372

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2020-08-21 20:00 +0200
Message-ID<AGjuy-7ye-9@gated-at.bofh.it>
In reply to#226370
On Fri, Aug 21, 2020 at 07:49:07PM +0200, john doe wrote:
> Hello all,
> 
> I'm trying to use the command provided at (1):
> 
> $ sed '/pattern{N;N;a try\d10}' input-file
> sed: -e expression #1, char 0: unmatched `{'

Are you missing a second / character after the pattern?

Why are you obfuscating the command?  Why not SHOW the actual command?

> As I understanded, I should make a newline before the right brace.
> I thought that '\d10' should do it but as shown above it does not.

I have no idea how you came up with that.

If you're doing this from bash, you could use bash's special $'...'
quoting to pass a newline encoded as \n .

Otherwise, put an ACTUAL newline inside the quotes.  You know, by
pressing the Enter key.  That's portable to every sh family shell.

[toc] | [prev] | [next] | [standalone]


#226373

Fromjohn doe <johndoe65534@mail.com>
Date2020-08-21 20:10 +0200
Message-ID<AGjEd-7QM-1@gated-at.bofh.it>
In reply to#226372
On 8/21/2020 7:51 PM, Greg Wooledge wrote:
> On Fri, Aug 21, 2020 at 07:49:07PM +0200, john doe wrote:
>> Hello all,
>>
>> I'm trying to use the command provided at (1):
>>
>> $ sed '/pattern{N;N;a try\d10}' input-file
>> sed: -e expression #1, char 0: unmatched `{'
>
> Are you missing a second / character after the pattern?
>
> Why are you obfuscating the command?  Why not SHOW the actual command?
>

Yes, I'm missing a '/'.

the command is:

$ sed '/line1/{N;N;a line-to-add}' input-file

>> As I understanded, I should make a newline before the right brace.
>> I thought that '\d10' should do it but as shown above it does not.
>
> I have no idea how you came up with that.
>
> If you're doing this from bash, you could use bash's special $'...'
> quoting to pass a newline encoded as \n .
>

So something like the following:

$ sed '/line1/{N;N;a line-to-add$'\n'}' input-file

> Otherwise, put an ACTUAL newline inside the quotes.  You know, by
> pressing the Enter key.  That's portable to every sh family shell.
>

Actually, I do not know, that is why  I'm  asking in here! :)

--
John Doe

[toc] | [prev] | [next] | [standalone]


#226376

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2020-08-21 20:40 +0200
Message-ID<AGk7f-805-3@gated-at.bofh.it>
In reply to#226373
On Fri, Aug 21, 2020 at 08:09:26PM +0200, john doe wrote:
> On 8/21/2020 7:51 PM, Greg Wooledge wrote:
> > If you're doing this from bash, you could use bash's special $'...'
> > quoting to pass a newline encoded as \n .
> 
> So something like the following:
> 
> $ sed '/line1/{N;N;a line-to-add$'\n'}' input-file

No.  Use $'...' instead of '...'.

sed '/line1/{N;N;a line-to-add\n}' input-file

> > Otherwise, put an ACTUAL newline inside the quotes.  You know, by
> > pressing the Enter key.  That's portable to every sh family shell.
> 
> Actually, I do not know, that is why  I'm  asking in here! :)

The Subject: header is a bit ambiguous, because you mention "POSIX
compliant sed", but you're asking on debian-user, where sed is not
necessarily POSIX compliant (GNU sed is notorious for having many
extensions).

Morever, it's not clear whether your operating environment is also
a POSIX *shell*.

Using a literal newline inside the single quotes is POSIX shell
compatible.  $'...' is not.

[toc] | [prev] | [next] | [standalone]


#226377

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2020-08-21 20:40 +0200
Message-ID<AGk7f-805-1@gated-at.bofh.it>
In reply to#226376
On Fri, Aug 21, 2020 at 02:35:35PM -0400, Greg Wooledge wrote:
> No.  Use $'...' instead of '...'.
> 
> sed '/line1/{N;N;a line-to-add\n}' input-file

Crap.  Of course I meant to write

sed $'/line1/{N;N;a line-to-add\n}' input-file

[toc] | [prev] | [next] | [standalone]


#226380

Fromjohn doe <johndoe65534@mail.com>
Date2020-08-21 21:00 +0200
Message-ID<AGkqB-86N-1@gated-at.bofh.it>
In reply to#226377
On 8/21/2020 8:37 PM, Greg Wooledge wrote:
> On Fri, Aug 21, 2020 at 02:35:35PM -0400, Greg Wooledge wrote:
>> No.  Use $'...' instead of '...'.
>>
>> sed '/line1/{N;N;a line-to-add\n}' input-file
>
> Crap.  Of course I meant to write
>
> sed $'/line1/{N;N;a line-to-add\n}' input-file
>

Okay, it uses the same syntax as for a subshell '$()'.

Next step is to find a way in which I can use variable substitution.

I appriciate your help Greg.


--
John Doe

[toc] | [prev] | [next] | [standalone]


#226382

Fromjohn doe <johndoe65534@mail.com>
Date2020-08-21 21:10 +0200
Message-ID<AGkAi-8po-7@gated-at.bofh.it>
In reply to#226380
On 8/21/2020 9:00 PM, Greg Wooledge wrote:
> On Fri, Aug 21, 2020 at 08:51:44PM +0200, john doe wrote:
>> Okay, it uses the same syntax as for a subshell '$()'.
>
> No, one of them is $'' and the other is $().  They have nothing in common.
>
> One of them is a form of quoting.  It acts just like '' except that it
> performs various backslash expansions.
>
> The other is a command substitution.
>
>> Next step is to find a way in which I can use variable substitution.
>
> THIS IS WHY YOU SHOW THE **ACTUAL** COMMAND instead of this obufuscated
> bullshit.
>

When I can not get the command I want, I break it down to the simplest
command as possible then I build from there to the command I realy want.

Thanks anyway.

--
John Doe

[toc] | [prev] | [next] | [standalone]


#226422

FromAndrei POPESCU <andreimpopescu@gmail.com>
Date2020-08-22 18:40 +0200
Message-ID<AGEIG-3wi-13@gated-at.bofh.it>
In reply to#226382

[Multipart message — attachments visible in raw view] — view raw

On Vi, 21 aug 20, 21:06:05, john doe wrote:
> 
> When I can not get the command I want, I break it down to the simplest
> command as possible then I build from there to the command I realy want.

Have you considered that solution(s) found might not be usable in the 
bigger context, basically wasting your time (as well as of those trying 
to help you)?

Kind regards,
Andrei
-- 
http://wiki.debian.org/FAQsFromDebianUser

[toc] | [prev] | [next] | [standalone]


#226429

Fromjohn doe <johndoe65534@mail.com>
Date2020-08-22 20:50 +0200
Message-ID<AGGKt-4H6-7@gated-at.bofh.it>
In reply to#226422
On 8/22/2020 6:33 PM, Andrei POPESCU wrote:
> On Vi, 21 aug 20, 21:06:05, john doe wrote:
>>
>> When I can not get the command I want, I break it down to the simplest
>> command as possible then I build from there to the command I realy want.
>
> Have you considered that solution(s) found might not be usable in the
> bigger context, basically wasting your time (as well as of those trying
> to help you)?
>

Actually, Greg has provided me with what I was looking for.

So thanks to Greg and to you for your input.

--
John Doe

[toc] | [prev] | [next] | [standalone]


#226384

FromGreg Wooledge <wooledg@eeg.ccf.org>
Date2020-08-21 21:10 +0200
Message-ID<AGkAi-8po-9@gated-at.bofh.it>
In reply to#226380
On Fri, Aug 21, 2020 at 08:51:44PM +0200, john doe wrote:
> Okay, it uses the same syntax as for a subshell '$()'.

No, one of them is $'' and the other is $().  They have nothing in common.

One of them is a form of quoting.  It acts just like '' except that it
performs various backslash expansions.

The other is a command substitution.

> Next step is to find a way in which I can use variable substitution.

THIS IS WHY YOU SHOW THE **ACTUAL** COMMAND instead of this obufuscated
bullshit.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web