image
image

Go Back   macosx.com > Mac Help Forums > Unix & X11

Reply
 
Thread Tools
  #1  
Old July 25th, 2007, 04:14 AM
Registered User
 
Join Date: Feb 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
kyiosus is on a distinguished road
Search and replace using bash script?

Hello, I'm kind of a bash newbie, so I'm wondering if anyone could help me out on this. Say I have a txt file saved somewhere, and I would like to replace certain words within that file (using the terminal) and then copy the file with the replaced words somewhere else. Is this possible? Thank you for your help!
Reply With Quote
  #2  
Old July 25th, 2007, 09:45 AM
macbri's Avatar
Mac (r)evolution
 
Join Date: Jun 2005
Location: Ireland
Posts: 251
Thanks: 1
Thanked 0 Times in 0 Posts
macbri is on a distinguished road
The 'sed' command will do the trick. Say for example you have a text file "input.txt" and you want to replace every occurance of the word "Hello" with "Goodbye". The output will be in "output.txt". In the terminal, you can do:

Code:
sed 's/Hello/Goodbye/g' input.txt > output.txt
In the sed command string "s/Hello/Goodbye/g", 's' means substitute, and the 'g' means substitute every occurance. If you omit the g only the first occurance of "Hello" on any given line will be replaced.

Check out the man page (in the terminal issue the command "man sed' without the quotes), or do a web search for "sed" for more information.

Last edited by macbri; July 25th, 2007 at 12:55 PM.
Reply With Quote
  #3  
Old July 25th, 2007, 12:18 PM
Registered User
 
Join Date: Feb 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
kyiosus is on a distinguished road
Ha! Works perfectly. Thank you so much for your help.
Reply With Quote
Reply

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT -5. The time now is 11:46 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.