sed help

Dan Nelson dnelson at allantgroup.com
Wed Jun 17 21:07:10 UTC 2009


In the last episode (Jun 17), chloe K said:
> 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

Try:

cat list.txt | while read name address ; do
  sed -e "s/Chloe/$name/ ; s/CA/$address/" < letter.txt > letter.$name.txt
done

You need the "while read" part so that you loop once per line.  Your code
would have looped once per word in the input file.  You also need
double-quotes on your sed line because $variable expansion isn't done inside
single-quotes.  If your names have spaces in them, consider swapping the
name and address in your input file, since "read" splits on spaces and
assigns the remainder of the line to the last variable listed.


-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-questions mailing list