how can i EDIT file by BASH
Hi every body
i have one question in BASH , how can write script can EDIT file ? if i want to edit /etc/file.conf and if the script find this word " cool " delete it or change it etc.. how can i do that? regardes .. |
With sed.
http://www.student.northpark.edu/pem...d/sed1line.txt http://www.grymoire.com/Unix/Sed.html For your purpose you will need sed's 'substitute' command. |
Thanks alien ..
but can you make small example , because i can't find what i wanna in sed's command and thank again |
i think i find what i look for
but i need example please that's what i mean (sed 's/abc/(abc)/' <old >new) so let's say the file i want edit it call /etc/file.conf and the word i want to replace it in the /etc/file.conf " moon " i want replace moon with "sun" so how can i do that? regardes.. |
You'd do something like:
sed 's/moon/sun/g' /etc/file.conf > /etc/file.conf To be safe you might want to direct the output of sed to another file instead of /etc/file.conf. Then copy the new file in place of /etc/file.conf (probably a better idea) |
Quote:
Quote:
|
Quote:
sed 's/moon/sun/g' /etc/file.conf > /etc/file.conf.new and then replacing file.conf with file.conf.new is the way. |
Quote:
The -i.bak switch allows you to edit the file in-place, and also makes a backup file with extension .bak just in case. |
Thanks alen and jlliagre but i am still have problem
the command sed 's/moon/sun/g' /etc/file.conf > /etc/file.conf.new is good and working now but i can't put it in the script if i wanna change moon by the script what i must do? ########################################## echo -n "put word" read word sed 's/$word/sun/g' /etc/file.conf > /etc/file.conf.new mv /etc/file.conf.new /etc/file.conf ########################################## that's really what i want .. but it's not working i don't know why any ideas? regardes.. |
Don't use single-quotes.
Code:
echo -n "put word" |
Yes yes yes
that's it.. special Thanks cs-cam and thanks for all :) |
i have new question about command sed
if i want to clear the /etc/file.conf ? how can i do that? i am trying something like that ############ sed -e "s/'*'/ /g" /etc/file.conf > /etc/file.conf mv /etc/file.conf /etc/file.conf ############ and it's worked but then i got error message.. any suggestions ? |
Quote:
Code:
>/etc/file.conf Quote:
The first line has already been discussed. The second one is meaningless, what is the point of moving to the same place (or renaming with the same name ...) ? |
Quote:
rm -f /etc/file.conf touch /etc/file.conf or echo an empty string to it: echo -n > /etc/file.conf |
Quote:
What's wrong with "> /etc/file.conf" ? |
All times are GMT -5. The time now is 05:44 AM. |