LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-19-2006, 06:27 AM   #1
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Rep: Reputation: 15
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 ..
 
Old 03-19-2006, 06:32 AM   #2
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.
 
Old 03-19-2006, 07:18 AM   #3
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Thanks alien ..

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

and thank again
 
Old 03-19-2006, 07:50 AM   #4
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Original Poster
Rep: Reputation: 15
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..
 
Old 03-19-2006, 01:09 PM   #5
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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)

Last edited by alienDog; 03-19-2006 at 01:10 PM.
 
Old 03-19-2006, 01:41 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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 !
 
Old 03-19-2006, 01:52 PM   #7
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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.

Last edited by alienDog; 03-19-2006 at 01:54 PM.
 
Old 03-19-2006, 05:19 PM   #8
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 46
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.
 
Old 03-19-2006, 09:03 PM   #9
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Original Poster
Rep: Reputation: 15
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..

Last edited by Guru Mind; 03-19-2006 at 09:04 PM.
 
Old 03-19-2006, 09:59 PM   #10
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 56
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
 
Old 03-19-2006, 10:08 PM   #11
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Yes yes yes

that's it..

special Thanks cs-cam and thanks for all
 
Old 03-20-2006, 11:23 AM   #12
Guru Mind
Member
 
Registered: Dec 2005
Posts: 41

Original Poster
Rep: Reputation: 15
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 ?
 
Old 03-20-2006, 12:05 PM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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 ...) ?
 
Old 03-20-2006, 12:19 PM   #14
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 46
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

Last edited by alienDog; 03-20-2006 at 12:20 PM.
 
Old 03-20-2006, 01:41 PM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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" ?
 
  


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
bash: how to edit cat <filename> | grep <keyword> feature? sirpelidor Linux - Software 2 06-20-2005 02:00 PM
Perl or Bash Mass File Edit redneon Programming 1 10-14-2004 11:43 AM
Line wrap errors with ssh / xterm / bash and up-arrow to edit brycen Linux - Software 0 09-22-2004 06:01 PM
how to edit file from bash commands legend1079 Linux - Newbie 3 03-03-2004 01:59 PM
Bash script to edit passwd without Root password? NetFlash Linux - Newbie 2 11-29-2003 04:22 PM


All times are GMT -5. The time now is 02:25 AM.

Main Menu
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