[talk] insert line after matched pattern (fwd)

N.J. Thomas njt at ayvali.org
Wed Aug 19 12:30:01 EDT 2015


* <freebsd at fongaboo.com> [2015-08-19 11:59:25-0400]:
> Anyone know how to insert a line after a matched pattern using sed,
> awk or any other method that gets the job done? Any tutorials I find
> seem not to work in BSD due to GNU sed vs. non-GNU?

The sed command to use is (a)ppend:

    $ cat foo
    line1
    line2
    line3

    $ cat foo|sed  '/^line2/a\
    foo
    '
    line1
    line2
    foo
    line3

Note that BSD version above (I'm running this on FreeBSD 10.1) requires
a new line after the backslash delimeter, whereas GNU sed doesn't:

    $ cat foo|gsed  '/^line2/a\foo'
    line1
    line2
    foo
    line3

You can even leave out the \ after the a with GNU sed, and it won't
complain (unless you run with --posix).

hth,
Thomas



More information about the talk mailing list