LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-11-2009, 06:23 AM   #1
another 1
Member
 
Registered: Nov 2008
Posts: 65

Rep: Reputation: 15
[solved] how to replace a line in a file with another line.


hi. i want to search for a specific word in a file and then replace the entire line with another line.
the solution presented in the other posts save the new line in a new file. i want to make the change in the same file.

( I am very new to Linux, this is a part of a school project blah blah blah)

for example

if the file contain the following line
default (and unspecified number of spaces) (the key word)
i want the output is as follow

# default (and unspecified number of spaces) (the key word)

that is to comment the line (the problem is that default is in every line so i can't use
sed 's/default/#default/'

(and the number of spaces is not specified).
another question
i want to append a new line to the end of the file if this line is not exist in the file
using

echo "the line" >> the file
will append the line even if it is already exist.

could you give me a shell script that do this. and could you please point me to a good book about the shell script to use it in the future.

thanks in advance.

Last edited by another 1; 04-11-2009 at 07:38 AM. Reason: solved
 
Old 04-11-2009, 06:55 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
1. Check this very good tutorial on sed: http://www.grymoire.com/Unix/Sed.html
Look for the ampersand sign (&) in the tutorial.

2. http://tldp.org/LDP/abs/html/
 
Old 04-11-2009, 07:01 AM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
What you want is to use a regular expression to address the line, then use sed's -i flag to edit in place:
Code:
sed -i '/the key word/ s/^/#/' filename
This will replace the beginning of the line (^) with a hash symbol where a match for the expression '/the key word/' is found. If the KW consists only of normal alpha-numeric characters, it is simple. If it contains any special characters (such as &) you would need to escape these with a backslash.

There are other ways to do it, but I think this is about the neatest. For more on sed, see here.
 
Old 04-11-2009, 07:21 AM   #4
another 1
Member
 
Registered: Nov 2008
Posts: 65

Original Poster
Rep: Reputation: 15
thanks you very much for all of you
thank you Robhogg

you way is better. i found a way in a site but it takes many lines and replacing for the text file
it is in the site http://www.grymoire.com/Unix/Sed.html#uh-42
#!/bin/sh
sed '
/WORD/ c\
Replace the current line with the line
'
this code doesn't save in the same file (when i tried that it resulted in an empty file) i modified the code to make save in another file, then deleting the old file and renaming the new file with the old name. so stupid way,so thank you very much.
for the second part i will use this stupid way to replace the line with an empty space and then add the line that i want. if you know other efficient way, can you tell me please.
 
Old 04-11-2009, 07:37 AM   #5
another 1
Member
 
Registered: Nov 2008
Posts: 65

Original Poster
Rep: Reputation: 15
thanks it is solved.also because of you Robhogg. the site that i have mentioned above give something about deleting line of the file. i couldn't use their code until that i noticed the -i in your command. i thing it is about saving the output in the same file. thnks
 
Old 04-11-2009, 09:09 AM   #6
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
Quote:
Originally Posted by another 1 View Post
i couldn't use their code until that i noticed the -i in your command. i thing it is about saving the output in the same file. thnks
Yes, it is. Not every version of sed has the -i option (for example, the one on the Solaris servers we have at work), so it won't always be mentioned in how-tos. A similar effect to using -i can be obtained by:
Code:
sed '/expression/ s/^/#/' file > temp_file && \
mv temp_file file
... which writes the output of sed to a temporary file,then if (and only if) sed completes without errors, overwrites the original with the temporary file. One trap to watch out for is that you can't simply redirect the output to the original file:
Code:
sed '/expression/ s/^/#/ file > file # Don't do this!
... as the redirect operator immediately truncates the file before sed gets a chance to read it.().

Last edited by Robhogg; 04-11-2009 at 09:12 AM.
 
  


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 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
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 02:43 PM
Replace line in file with two lines?! Possible? eur0dad Linux - General 2 09-07-2006 03:15 PM


All times are GMT -5. The time now is 06:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration