sed help

Giorgos Keramidas keramida at
Wed Jun 17 21:17:54 UTC 2009

On Wed, 17 Jun 2009 10:55:28 -0700 (PDT), chloe K <chloekcy2000 at> wrote:
> Hi
> I have a file. list.txt (two columns)
> column1    column2
> name        address
> I need to put in the letter file letter.txt eg:
> Dear: Chloe
> Address: CA
> Can I use this
> for i `cat list.txt` | sed 's/Chloe/$i.1; /CA/$i.2/g' $i.letter.txt

No that won't work.  sed does 'stream editing' to its own input file, so
you have to redirect each output for *every* loop iteration.  But I
don't think this is a good method of solving this problem, because you
only have one input file and one output file.

See what the following does, to give you can idea:

$ echo giorgos keramida at | sed -e 's/^\([^ ]*\)[ ]*\(.*\)$/\
Dear:    \1\
Address: \2\

NOTE: If you really want to work effectively with sed, please take a bit
of time to read the manpage of sed(1) and ed(1), paying careful to the
parts about: (1) regular expressions, (2) character classes, and (3) the
rules of character quoting.

It's also worth noting that you don't _have_ to use sed for this
specific problem, because there are other tools more suitable for
processing data in columns, i.e. awk(1):

$ echo giorgos keramida at | \
    awk '{print "Dear:   ", $1; print "Address:", $2}'
Dear:    giorgos
Address: keramida at

A single line of awk is vastly more readable than the equivalent sed
expression in this case.

More information about the freebsd-questions mailing list