LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how can i EDIT file by BASH (https://www.linuxquestions.org/questions/programming-9/how-can-i-edit-file-by-bash-426229/)

Guru Mind 03-19-2006 06:27 AM

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

alienDog 03-19-2006 06:32 AM

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.

Guru Mind 03-19-2006 07:18 AM

Thanks alien ..

but can you make small example , because i can't find what i wanna in sed's command

and thank again

Guru Mind 03-19-2006 07:50 AM

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

alienDog 03-19-2006 01:09 PM

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)

jlliagre 03-19-2006 01:41 PM

Quote:

Originally Posted by alienDog
You'd do something like:

sed 's/moon/sun/g' /etc/file.conf > /etc/file.conf

Ouch, that is first blanking the file.conf file, then processing it ... too late.
Quote:

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)
Definitely !

alienDog 03-19-2006 01:52 PM

Quote:

Ouch, that is first blanking the file.conf file, then processing it ... too late.
Right, so it seems, I tested and that's what it does really. Using cat to pass the file to sed helps, but I'd still go for using an alternative file for output. So:

sed 's/moon/sun/g' /etc/file.conf > /etc/file.conf.new

and then replacing file.conf with file.conf.new is the way.

/bin/bash 03-19-2006 05:19 PM

Quote:

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..
sed -i.bak 's/cool//g' /etc/file.conf

The -i.bak switch allows you to edit the file in-place, and also makes a backup file with extension .bak just in case.

Guru Mind 03-19-2006 09:03 PM

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

cs-cam 03-19-2006 09:59 PM

Don't use single-quotes.
Code:

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


Guru Mind 03-19-2006 10:08 PM

Yes yes yes

that's it..

special Thanks cs-cam and thanks for all :)

Guru Mind 03-20-2006 11:23 AM

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 ?

jlliagre 03-20-2006 12:05 PM

Quote:

Originally Posted by Guru Mind
i have new question about command sed

if i want to clear the /etc/file.conf ? how can i do that?

Like this:
Code:

>/etc/file.conf
Quote:

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 ?
Not sure about what do you want to achieve with this bogus code.
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 ...) ?

alienDog 03-20-2006 12:19 PM

Quote:

Originally Posted by Guru Mind
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 ?

To clear file completely just remove and re-create it:

rm -f /etc/file.conf
touch /etc/file.conf

or echo an empty string to it:

echo -n > /etc/file.conf

jlliagre 03-20-2006 01:41 PM

Quote:

Originally Posted by alienDog
To clear file completely just remove and re-create it:

rm -f /etc/file.conf
touch /etc/file.conf

or echo an empty string to it:

echo -n > /etc/file.conf

So you reject my suggestion because it's ... too simple ?

What's wrong with "> /etc/file.conf" ?


All times are GMT -5. The time now is 05:44 AM.