LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using sed command (http://www.linuxquestions.org/questions/linux-general-1/using-sed-command-4175451086/)

ahsanpmd 02-21-2013 04:50 AM

Using sed command
 
I want to replace ydate_ini='2013022000' with ydate_ini='$DATE00' (where DATE is system's date) in a file using sed.

Tried cat INPUT | sed "s/^.ydate_ini.*$/ ydate_ini='$DATE00'/" > INPUT.new

but now working.

chrism01 02-21-2013 05:52 AM

Code:

# create date
 t=`date +%Y%m%d`
 echo $t
20130221


 cat t.t
ydate_ini='2013022000'

 sed -i -e "s:^ydate_ini.*:ydate_ini='${t}00':" t.t
 cat t.t
ydate_ini='2013022100'

-i flag does in-place replacement, so backup the file first.

ahsanpmd 02-21-2013 06:56 AM

Thanks chris atually I was making a mistake in my command , thanks For your help too

ahsanpmd 02-21-2013 10:11 PM

Your command not working Chris
In my file INPUT there is line ydate_ini='2013022000', ydate_bd='2013022000',
I have to change both ydate_ini and ydate_bd with current date. Your command isn't making any effect.However I have used
cat INPUT | sed "s/^.*ydate_ini=.*$/ ydate_ini='$DAY'/" > INPUT.new
It changes ydate_ini to current date but it omit the next portion of the line i.e ydate_bd='2013022000',

ahsanpmd 02-21-2013 10:23 PM

it worked
cat INPUT | sed "s/^.*ydate_ini=.*$/ ydate_ini='$DAY', ydate_bd='$DAY',/" > INPUT.new


All times are GMT -5. The time now is 09:38 PM.