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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Passing the Output of grep to sed command - to find and replace a string in a file.
# 1  
Old 08-19-2014
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 :
Code:
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
Code:
grep '[0-9]' a.txt

which gives the output as :
Code:
$$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.

Last edited by rbatte1; 08-19-2014 at 01:50 PM..
# 2  
Old 08-19-2014
try
Code:
sed 's/20140818/20140819/g' a.txt > b.txt && mv b.txt a.txt

# 3  
Old 08-19-2014
Code:
[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

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 08-19-2014
Code:
sed "s#\(.*=\)\(.*\)#\1$(date +%Y%m%d)#" file

# 5  
Old 08-19-2014
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.

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

output will be as follows.

Code:
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.

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

Thanks,
R. Singh

Last edited by rbatte1; 08-19-2014 at 01:52 PM.. Reason: RavinderSingh13 Added one more solution - RBATTE1 for spelling
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 08-19-2014
Quote:
Originally Posted by balajesuri
Code:
[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

Quote:
Originally Posted by Yoda
Code:
sed "s#\(.*=\)\(.*\)#\1$(date +%Y%m%d)#" file

Quote:
Originally Posted by RavinderSingh13
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 outpput.

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

output will be as follows.

Code:
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.

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

Thanks,
R. Singh
Solved. You people are my heroes!! Thanks!!

Cheers.
Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

3. Shell Programming and Scripting

Sed: find and replace backwards, until string

Some help please: Need to find string ||(everything in front of it)B0300|| and replace it with ||0|| globally In: 16112121||||0||0||0||0||0||52||52||0||0||0||0||1507200053342B0300||1507200053342B0300||0||0||0||0700 Out: 16112121||||0||0||0||0||0||52||52||0||0||0||0||0||0||0||0||0||0700 ... (4 Replies)
Discussion started by: drbiloukos
4 Replies

4. Shell Programming and Scripting

Grep and sed (replace string in patterned lines)

Grep and Sed (replace string in patterned lines) Hi all, I want to grep for "PATTERN" and only if "PATTERN" is in a line, this line shall be used as replacement input e.g. for SED. I don't get it running in one line. NOT RUNNING - just first idea... I don't know how to redirect grep... (2 Replies)
Discussion started by: unknown7
2 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

8. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

9. UNIX for Dummies Questions & Answers

how to use sed or perl command to find and replace a directory in a file

how to use sed command to find and replace a directory i have a file.. which contains lot of paths ... for eg.. file contains.. /usr/kk/rr/12345/1 /usr/kk/rr/12345/2 /usr/kk/rr/12345/3 /usr/kk/rr/12345/4 /usr/kk/rr/12345/5 /usr/kk/rr/12345/6 /usr/kk/rr/12345/7... (1 Reply)
Discussion started by: wip_vasikaran
1 Replies

10. Shell Programming and Scripting

How to replace all string instances found by find+grep

Hello all Im performing find + grep operation that looks like this : find . -name "*.dsp" | xargs grep -on Project.lib | grep -v ':0' and I like to add to this one liner the possibility to replace the string " Project.lib" that found ( more then once in file ) with "Example.lib" how can I do... (0 Replies)
Discussion started by: umen
0 Replies
Login or Register to Ask a Question