LinuxQuestions.org
Help answer threads with 0 replies.
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 12-10-2005, 01:49 AM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,256

Rep: Reputation: 53
How can I use a shell script to add and replace lines in a file?


How can I use a shell script to replace lines in a file?

I want the script to:
replace
deliver_queue_load_max = 5
with
deliver_queue_load_max = 25
in exim.conf

and replace
Timeout 300
with
Timeout 60
and
KeepAlive Off
with
KeepAlive On
in httpd.conf

added
#RLimitMEM 203961002
#RLimitCPU 240
after
Alias /bandwidth/ /usr/local/bandmin/htdocs/
in httpd.conf

How can I do this?

Thanks in advance
 
Old 12-10-2005, 08:28 AM   #2
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
sed is the answer to your question.
Find a good tutorial like this short primer and have alook at the man and info pages...
 
Old 12-11-2005, 05:33 PM   #3
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Quote:
Originally Posted by abefroman
How can I use a shell script to replace lines in a file?

I want the script to:
replace
deliver_queue_load_max = 5
with
deliver_queue_load_max = 25
in exim.conf

and replace
Timeout 300
with
Timeout 60
and
KeepAlive Off
with
KeepAlive On
in httpd.conf

added
#RLimitMEM 203961002
#RLimitCPU 240
after
Alias /bandwidth/ /usr/local/bandmin/htdocs/
in httpd.conf

How can I do this?

Thanks in advance
Take a look here.
Something like
Code:
"sed s/deliver_queue_load_max\ =\ 5/deliver_queue_load_max\ =\ 25/g"
should work. You might need a "\" before "=", I'm not sure about this.
 
Old 12-17-2005, 02:32 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by zahadumy
Take a look here.
Something like
Code:
"sed s/deliver_queue_load_max\ =\ 5/deliver_queue_load_max\ =\ 25/g"
should work. You might need a "\" before "=", I'm not sure about this.

you neednot prefix a "\" before "="
it would work without that
Code:
echo "deliver_queue_load_max = 5" | sed -e 's/deliver_queue_load_max = 5/deliver_queue_load_max = 25/'
 
Old 12-17-2005, 03:22 AM   #5
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
Have you checked that? Actually the \ is not for = but to escape the white-space.

Last edited by linmix; 12-17-2005 at 03:23 AM.
 
Old 12-17-2005, 07:34 AM   #6
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Quote:
Originally Posted by linmix
Have you checked that? Actually the \ is not for = but to escape the white-space.
As far as I know, \ is used to escape any character that the shell would construe and you want it construed by the sed command. I'm pretty sure about that...
 
Old 12-17-2005, 08:01 AM   #7
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
yes, but have a look at the placing. In both cases where \ is use it is located before a space, so in this instance it is not = that's being escaped, but the spaces.
 
Old 12-19-2005, 05:21 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
Code:
#!/usr/bin/perl -wna

# splits into an array, -1 is the last field

$F[-1] = "XXX" if m/Timeout/;
$F[-1] = "XXX" if m/KeepAlive/;
$F[-1] = "XXX" if m/deliver_queue_load_max/;

# you get the idea?


print "@F\n" ;
perl -i.bak <script> <file>
will edit the file and create a backup .bak
 
Old 12-19-2005, 05:31 AM   #9
denver1980
Member
 
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48

Rep: Reputation: 15
You only need to use \ before "system" caracters if you don't want sed to interpret them...
like :
^ = start of line
$ = end of line
so if you want to replace :
My ^ gives me $$
by :
Hello world
Code:
echo 'My ^ gives me $$' | sed 's/My \^ gives me \$\$/Hello world/'
 
Old 12-21-2005, 04:01 AM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by linmix
Have you checked that? Actually the \ is not for = but to escape the white-space.
yes i had checked that.

its working as i had expected.

why to provide a '\' to escape a white-space character which isnt a special character?

i believe i am right.
 
Old 12-27-2005, 05:05 PM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
The backslash character isn't needed in your case because the sed expression is contained in single quotes. Suppose the name of the file you were changing was named "http one.conf" in that case you would either need to escape the space, or put the argument in quotes. If the filename contained the "$" character, then you would need either a backslash or single quotes.

All of the whitespace characters and special characters are listed in the bashref manual.
 
  


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
Shell Script: Add Users from file flobadon Programming 3 12-07-2004 03:49 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM
shell script - add file to archive ? xconspirisist Linux - Software 2 03-20-2004 03:27 PM
Replace lines in script AMMullan Linux - Software 9 12-06-2003 11:36 PM


All times are GMT -5. The time now is 04:56 PM.

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