From: WH on
Dear all,

I am looking for a good way to replace some strings in a file according
to a dictionary saved in another file.

Suppose the dictionary file has the following format

func1 help
func4 close
....

The first column is the index and the second column is the value. The
index strings should have no space.

Suppose a file has a string:

"Keyboard has func1 and func4 ..."

I need the output to be

"Keyboard has help and close ..."

Any suggestions?

Thanks a lot!
-WH
From: Ed Morton on
On 5/13/2010 6:59 PM, WH wrote:
> Dear all,
>
> I am looking for a good way to replace some strings in a file according
> to a dictionary saved in another file.
>
> Suppose the dictionary file has the following format
>
> func1 help
> func4 close
> ...
>
> The first column is the index and the second column is the value. The
> index strings should have no space.
>
> Suppose a file has a string:
>
> "Keyboard has func1 and func4 ..."
>
> I need the output to be
>
> "Keyboard has help and close ..."
>
> Any suggestions?
>
> Thanks a lot!
> -WH

One simple way would be:

awk 'NR==FNR{map[$1]=$2;next} {for (i=1;i<=NF;i++) if ($i in map) $i=map[$i]}1'
file1 file2

if file2s text is space-separated as shown in your posted sample and you don't
care if contiguous chains of spaces get compressed to one blank char and the
value strings in file1 don't contain spaces.

Ed.