Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :

SomeTextGoesHere
[ASD.EXAMPLE@ABCD]
$$TODAY_DT=20140818

[FGH.EXAMPLE@ABCD]
$$TODAY_DT=20140818

[QWE.EXAMPLE@ABCD]
$$TODAY_DT=20140818

I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script.
(It would even be better if I could pass both the file name, in which the date should be updated, and date as arguments)

I tried greping the line where the date occurs using

grep '[0-9]' a.txt

which gives the output as :

$$TODAY_DT=20140818
$$TODAY_DT=20140818
$$TODAY_DT=20140818

Now I don't know how to give this output as input to a sed command and replace the date(only date, the numeric part).

Or, is there any other alternative way to achieve this ??

Cheers.

try

sed 's/20140818/20140819/g' a.txt > b.txt && mv b.txt a.txt
[user@host ~]$ x=20140819; sed "s/=[0-9]\{8\}/=$x/" file
SomeTextGoesHere
[ASD.EXAMPLE@ABCD]
$$TODAY_DT=20140819

[FGH.EXAMPLE@ABCD]
$$TODAY_DT=20140819

[QWE.EXAMPLE@ABCD]
$$TODAY_DT=20140819

1 Like
sed "s#\(.*=\)\(.*\)#\1$(date +%Y%m%d)#" file

Hello,

Could you please try the following code, hope this helps in variable named s1 you can set the date value which you want to get in output.

awk -F"=" -vs1="20140827" '/^\$\$TODAY/ {$2=s1} 1' OFS="="  Filename

output will be as follows.

SomeTextGoesHere
[ASD.EXAMPLE@ABCD]
$$TODAY_DT=20140827

[FGH.EXAMPLE@ABCD]
$$TODAY_DT=20140827

[QWE.EXAMPLE@ABCD]
$$TODAY_DT=20140827

If you want to give today's date as passing variable then following may help.

awk -F"=" -vs1="$(date +%Y%m%d)" '/^\$\$TODAY/ {$2=s1} 1' OFS="="  filename

Thanks,
R. Singh

1 Like

Solved. You people are my heroes!! Thanks!!

Cheers.
:smiley: