LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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: 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:
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: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
Posts: 952

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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration