Re: Nice easy sed question

From: ax disroo <ax61_at_disroot.org>
Date: Fri, 12 Sep 2025 09:44:35 UTC
> On Sep 11, 2025, at 12:15 PM, Frank Leonhardt wrote:
> 
> I'm going quietly crazy here. BSD sed is it's own thing, but I can't see what I'm doing wrong...
> 
> I've got a file called example.txt:
> 
> Line 1
> Line 2
> Line 3
> 
> I'm trying to add "New Line" after "Line 2"
> 
> Both of these should work as far as I know:
> 
> sed  -i.bak  '/Line 2/a\New Line' example.txt
> sed  -i.bak  -e '/Line 2/a\" -e "New Line' example.txt
> 
> (BSD requires a backup specification for -i IIRC, or '')
> 
> If I run it with the -i and have a genuine newline after \ it does write the correct stuff to stdout:
> 
> # sed  '/Line 2/a\
> > New Line' example.txt
> Line 1
> Line 2
> New Line
> Line 3
…
> The Fine Manual says of 'a':
> 
>   [1addr]a\
>      text    Write text to standard output immediately before each attempt to
>              read a line of input, whether by executing the “N” function or by
>              beginning a new cycle.
> 
> This implies the newline is required but I'm struggling with finding a sane syntax here. A pointer to some better documentation would be welcome!
> 
> Or is there a better utility for editing (non-system) configuration files by script I just don't know about?

Use GNU sed instead; your first syntax certainly worked with v4.{5,9} (did not bother to try the second with two “-e”) …

cat <<X | gsed -e '/A/a\-C 8'
A 2
B 5
X

- ax