Subject: Re: shell script question (grep)
To: Lubos Vrbka <shnek@chemi.muni.cz>
From: Giorgos Keramidas <keramida@ceid.upatras.gr>
List: netbsd-help
Date: 02/11/2003 05:35:32
On 2003-02-09 18:32, Lubos Vrbka <shnek@chemi.muni.cz> wrote:
> > How many lines are there in file 2?
> it can vary, from small to large numbers (20000, but could be even more),
> but time isn't problem... :o)
>
> > while read i j < file2
> > do sed -e "s/$i\$/$j/" < file1 > file1.out
> > mv file1.out file1
> > done
> looks fine, thank you for help... i thought it could be somehow done using
> sed, but didn't know why :o)
>
> just one question. what if the numbers being replaced will look like:
> .... 580
> .... 1580
> .... 2580
>
> and in the "translation table" there will be
> 580    value1
> 1580  value2
> 2580  value3
> won't sed substitute it to
> value1, 1value1 and 2value1?
> should the sed command then be modified to
> "s/\ $i\$/\ $j/"
> to work properly?

You could force sed to treat $i and $j as full "words" by adding
\<...\> around them in the substitution pattern.  Seeing how you pass
$i as the last part of a line, you don't need the \> part either, and
that would make the script:

	while read i j < file2 ;do
		sed -e "s/\\<$i\$/$j/" < file1 > file1.out
		mv file1.out file1
	done

- Giorgos