Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-28-2009, 08:11 AM   #1
Registered: Sep 2006
Posts: 73

Rep: Reputation: 15
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

Old 03-28-2009, 08:26 AM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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:
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"
Old 03-28-2009, 08:26 AM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
Old 03-29-2009, 09:43 AM   #4
Registered: Jul 2004
Location: Chennai, India
Posts: 949

Rep: Reputation: 216Reputation: 216Reputation: 216

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.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM
shell script: insert line in a file noir911 Programming 6 02-04-2008 10:42 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 08:41 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:48 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration