LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to replace a line in a file with another line. (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-replace-a-line-in-a-file-with-another-line-718393/)

another 1 04-11-2009 05:23 AM

[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.

sycamorex 04-11-2009 05:55 AM

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/

Robhogg 04-11-2009 06:01 AM

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.

another 1 04-11-2009 06:21 AM

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.

another 1 04-11-2009 06:37 AM

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

Robhogg 04-11-2009 08:09 AM

Quote:

Originally Posted by another 1 (Post 3505409)
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.(:eek:).


All times are GMT -5. The time now is 08:28 PM.