help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Printing sections from files with sed


From: Kerin Millar
Subject: Re: Printing sections from files with sed
Date: Tue, 31 Jan 2023 10:49:31 +0000

On Tue, 31 Jan 2023 08:28:05 +0100 (CET)
Hans Lonsdale <hanslonsdale@mailfence.com> wrote:

> 
> 
> > ----------------------------------------
> > From: Kerin Millar <kfm@plushkava.net>
> > Date: Jan 31, 2023, 7:18:52 PM
> > To: Hans Lonsdale <hanslonsdale@mailfence.com>
> > Cc: Help Bash <help-bash@gnu.org>
> > Subject: Re: Printing sections from files with sed
> > 
> > 
> > On Tue, 31 Jan 2023 07:49:02 +0100 (CET)
> > Hans Lonsdale <hanslonsdale@mailfence.com> wrote:
> > 
> > > Here I set the matching patterns and call sed 
> > > 
> > > 
> > >       pn_ere='^[[:space:]]*([#;!]+|@c|//)[[:space:]]'
> > 
> > This contains unescaped slashes.
> > 
> > >       beg_re="${pn_ere}${faml} \[${asmb}\][[:space:]]*$"
> > >       end_re="${pn_ere}END OF ${faml} \[${asmb}\][[:space:]]*$"
> > > 
> > >       sed -n "/$beg_re/,/$end_re/ {
> > >           /$beg_re/d ; /$end_re/z; s/${pn_ere}// ; p
> > 
> > The s command here is using the slash as a delimiter.
> 
> I understood your discussion except this one.  How am I using slash as 
> delimiter?

By having elected to write the command so. The POSIX documentation explains all 
of this more effectively. Below is an excerpt from sed(1p) as to how the 's' 
command functions. You can also find it at 
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html.

[2addr]s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the pattern 
space. Any character other than <backslash> or <newline> can be used instead of 
a <slash> to delimit the BRE and the replacement. Within the BRE and the 
replacement, the BRE delimiter itself can be used as a literal character if it 
is preceded by a <backslash>.

Your options are to use a non-conflicting delimiter, backslash-escape the 
conflicting character in the pattern or to use some other approach whereby you 
don't inject patterns into code to begin with (that is, to use something other 
than sed).

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]