Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Jun 2009 16:07:07 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        chloe K <chloekcy2000@yahoo.ca>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: sed help
Message-ID:  <20090617210703.GC12966@dan.emsphone.com>
In-Reply-To: <974960.33077.qm@web57412.mail.re1.yahoo.com>
References:  <974960.33077.qm@web57412.mail.re1.yahoo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090617210703.GC12966>