sed replace

Hi,

i have a file as give below

>cat sample_file
param1 val1 2012-06-19 

##there can be one or more space after 2012-06-19 in the above file

i want to replace val1 with a with value passed through a variable...
below is the command i tried

>parval='param1 val2'
>par1=param1
>sed "s/$par1 ..* /$parval /g" < sample_file

param1 val2

ie, the whole line in the sample file is replaced (the date is removed) but i want only the param1 val1 to be replaced with the parval variable value

can anyone help me with a suitable sed command for this requirement

If I understand your requirement properly, try

sed "s|$par1[ ]*[^ ]*|$parval|" sample_file

.* expression will always match the longest possible pattern...need to restrict the matched pattern.

1 Like

this command is working ....thanks

can i know what does this mean

$par1[ ]*[^ ]*

It means a pattern which has the value of "par1" followed by 0 or more spaces and 0 or more characters which are not space...

1 Like