LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2009, 01:51 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,265

Rep: Reputation: 53
Find and replace with a shell script


What is best way to replace the 1 and the 3 below with a 0 using a shell script?

It is in a text file with lots of lines and similar lines:
lots of text
...
useHeloBlacklist:=1
DoIPinHelo:=3
...
lots of text

TIA!
 
Old 03-10-2009, 02:33 PM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
If the values 1 and 3 are always the same, this will work:

file.sed
Code:
s/useHeloBlacklist:=1/userHeloBlackList:=0/g
s/DoIPinHelo:=3/DoIPinHelo:=0/g
Code:
cat file | sed -f file.sed
If the values of for the above can change and you need to match and replace, the script will get a bit more difficult.
 
Old 03-10-2009, 02:41 PM   #3
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,265

Original Poster
Rep: Reputation: 53
Does sed support reg exps?

s/useHeloBlacklist:=[0-9]/userHeloBlackList:=0/g
s/DoIPinHelo:=[0-9]/DoIPinHelo:=0/g
 
Old 03-10-2009, 02:53 PM   #4
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,265

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by abefroman View Post
Does sed support reg exps?

s/useHeloBlacklist:=[0-9]/userHeloBlackList:=0/g
s/DoIPinHelo:=[0-9]/DoIPinHelo:=0/g
Looks like it does, however, what is the best way to write the output to a file?
When I do this:
cat assp.cfg | sed s/useHeloBlacklist:=[0-9]/useHeloBlacklist:=0/g >assp.cfg

It just zeros out assp.cfg

TIA
 
Old 03-10-2009, 03:26 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Try in place editing :

Code:
sed -i -e "s/useHeloBlacklist:=[0-9]/useHeloBlacklist:=0/g" -e "s/DoIPinHelo:=[0-9]/DoIPinHelo:=0/g" assp.cfg
- should do both subs at once.
 
Old 03-10-2009, 03:38 PM   #6
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,265

Original Poster
Rep: Reputation: 53
Quote:
Originally Posted by bgeddy View Post
Try in place editing :

Code:
sed -i -e "s/useHeloBlacklist:=[0-9]/useHeloBlacklist:=0/g" -e "s/DoIPinHelo:=[0-9]/DoIPinHelo:=0/g" assp.cfg
- should do both subs at once.
Thanks!

That worked.
 
  


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
[SOLVED] Find and Replace problem at Shell Script TzaB Programming 3 12-27-2008 06:16 AM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
Shell script to find/replace build new TAB record ljungers Programming 6 01-19-2007 04:47 PM
1. shell script "find and replace" on text 2. java GUI application randomx Programming 4 03-05-2004 01:01 PM


All times are GMT -5. The time now is 09:02 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