LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-28-2009, 08:11 AM   #1
amit_pansuria
Member
 
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

Amit
 
Old 03-28-2009, 08:26 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

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

Rep: Reputation: 108Reputation: 108
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
 
Old 03-29-2009, 09:43 AM   #4
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 606

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


Reply


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 51 07-04-2013 08:16 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


All times are GMT -5. The time now is 10:16 AM.

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