Friday, February 18, 2005

Append a new line with sed

I've write before about how much I like Perl ... well, related with Perl and command line you got sed wich is:
Ed is a line-oriented text editor, used to create, display, and modify text files (both interactively and via shell scripts). For most purposes, ed has been replaced in normal usage by full-screen editors (emacs and vi, for example).

You got a file and you want to append this new line under these pattern.

What I've done is

  1. create a file intructions.ed containing
    g/pattern/a\
    this new line
    w
    q

    ... and save this file
  2. assume that original file and intructions.ed are in the same path, then type
    ed original_file.txt < instructions.ed
  3. Done! you see a number at your screen, wich are bytes after and before been tracted by you intructions.ed file

sed are used for pattern substitutions but not usually for append a new line, that why I post this lines.
P.D There were a mistake at this entry I've fixed, you must finish with a backslah after final a (this a stands for append), otherwise it shouldn't perform anything...