LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-04-2010, 03:55 PM   #1
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Rep: Reputation: 0
shell script replace a line from a file


Hello,
I am beginner in this business of shell script and I have no idea how to do the following:
I would like to replace the lines of my file that contains 'CFL=' by 'CFL=0.5'.
Note that I want to replace the full line meaning
Thanks
 
Old 03-04-2010, 04:01 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Code:
sed -i s/CFL=/CFL=0.5/g yourFileName.ext
man sed

--- rod.
 
Old 03-04-2010, 04:03 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by dthanoon View Post
Hello,
I am beginner in this business of shell script and I have no idea how to do the following:
I would like to replace the lines of my file that contains 'CFL=' by 'CFL=0.5'.
Note that I want to replace the full line meaning
Thanks
Code:
sed -i 's/CFL=/CFL=0.5/g' path/to/your/file
Quote:
Note that I want to replace the full line meaning
Not sure what you mean by that.
 
Old 03-04-2010, 04:05 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You can do this using the `sed' program. This seems like it might be a homework problem, which is against LQ rules. Could you post what you try and then we can offer improvements. Also explain what you mean by "replace the full line meaning". From your description, I'm not certain if a line contains more than CFL= and if so, if the other contents need to be left intact or replaced as well.
 
Old 03-04-2010, 04:12 PM   #5
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Well if I do what you suggest it will replace 'CFL=' by 'CFL=0.5'
Lets say I have the following file:
-----
Data Values
CFL=0.2
r=9
...
----
I want to replace the line that contain 'CFL=' by 'CFL=0.5' so my output file should be:
-----
Data Values
CFL=0.5
r=9
...
----

My issue is to replace the whole line because I am not suppose what is the value assign to CFL
Is this more clear?
 
Old 03-04-2010, 04:19 PM   #6
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Code:
sed -i 's/\(CFL=\).*/\10.5/g' path/to/your/file
 
Old 03-04-2010, 04:29 PM   #7
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Code:
sed -i 's/\(CFL=\).*/\10.5/g' path/to/your/file
It works
Thank you so much.
Would you be kind enough to explain me what is the .*/\1 for?
Thanks again
 
Old 03-04-2010, 06:25 PM   #8
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Code:
sed -i 's/\(CFL=\).*/\10.5/g' path/to/your/file
Here is another question related to this:
if I want to do it in a loop
if I do the following
for ((i=0; i<=5; i++))
do
sed -i 's/\(CFL=\).*/\10.5$i/g' path/to/your/file
done
it will put the charachter '$i' intead of the number I want him to put.
Do you guys have any idea how to put the value coming from the loop?

Thanks
 
Old 03-05-2010, 05:40 AM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Sed runs each sed command on a line before reading the next line. It would make more sense to have a sed command for each instance, but what you want to do will match the previous substitution, ending up with CFL=10.55. The first iterations were wasting time.

The general answer is because you used single quotes. Another problem is that `$' stands for the end of line in regular expressions.

You need it to be expanded before the sed command is executed:
sed -i 's/\(CFL=\).*/10.5'"$I"'/g' path/to/your/file.
 
Old 03-05-2010, 08:12 AM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by jschiwal View Post
Sed runs each sed command on a line before reading the next line. It would make more sense to have a sed command for each instance, but what you want to do will match the previous substitution, ending up with CFL=10.55. The first iterations were wasting time.
Actually, it would end up with CFL=0.55. Notice the backslash.
 
Old 03-05-2010, 10:46 AM   #11
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
Sed runs each sed command on a line before reading the next line. It would make more sense to have a sed command for each instance, but what you want to do will match the previous substitution, ending up with CFL=10.55. The first iterations were wasting time.

The general answer is because you used single quotes. Another problem is that `$' stands for the end of line in regular expressions.

You need it to be expanded before the sed command is executed:
sed -i 's/\(CFL=\).*/10.5'"$I"'/g' path/to/your/file.
Thanks it is working indeed
The reason why I loop is that I will have some other command inside the loop that launch a computation calling the file I changed as input parameters, therefore I don't wast time, it's all the purpose of my script shell, launch many computation changing some input parameters.

Thanks again, it was a real help for me.
 
Old 03-05-2010, 10:46 AM   #12
dthanoon
LQ Newbie
 
Registered: Mar 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Actually, it would end up with CFL=0.55. Notice the backslash.
Indeed you are right. Thank you as well
 
  


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
How to replace line with another line in shell script pramod.srk Linux - Newbie 5 02-26-2010 07:00 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 09:49 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 11:11 AM


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

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration