LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to replace line of file with another line using awk in shell script (https://www.linuxquestions.org/questions/programming-9/how-to-replace-line-of-file-with-another-line-using-awk-in-shell-script-715075/)

amit_pansuria 03-28-2009 08:11 AM

how to replace line of file with another line using awk in shell script
 
I have a file that contains line


MXST6MXDT:Mexico City Daylight:5,1-7:+1:9,24-30:0

I want to replace above line with the following line

MXST6MXDT:Mexico City Daylight:4,1-7:+1:10,25-31:0

how do i do it using shell script

Amit

pixellany 03-28-2009 08:26 AM

If it's just one file, just edit it. The only reason for a script is if you need to do it several times.

To write a script, you need to define the decision logic. e.g.: Are you going to act on every line containing "Mexico City"? Or maybe you just want to change every instance of "5,1-7:+1:9,24-30:0" to "4,1-7:+1:10,25-31:0" ..?

Here is one of many possible solutions:
Code:

sed '/Mexico/s/5,1-7:+1:9,24-30:0/4,1-7:+1:10,25-31:0/ filename > newfilename
This makes the replacement on every line which contains "Mexico"

Hko 03-28-2009 08:26 AM

Code:

sed -i 's/MXST6MXDT:Mexico City Daylight:5,1-7:+1:9,24-30:0/MXST6MXDT:Mexico City Daylight:4,1-7:+1:10,25-31:0/' file.txt

AnanthaP 03-29-2009 09:43 AM

Hey.

These lookslike entries in the task scheduler. 'at`, `cron` etc. Why do it programatically when you can generate the code for 25th to 31st. Have a slightly different name and for both call the same functional routine.

End


All times are GMT -5. The time now is 11:49 AM.