DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 22nd September 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Thanked 20 Times in 18 Posts
Default Command to find and replace, but not creating a new file

So I have a file, with a bunch of pattern "foo", and I wanna replace them with "bar":

Code:
blah1 foo1 blah2 blah3 foo2 blah4foo3
We all know all the pattern can be replaced by `sed` command, the catch is, you need to create a "temp" file to hold the output.

I wouldnt bother if the file size is just a couple of Kb, but, what if its a few hundred of Mb, Im restricted in disk space, then you are in trouble.

I think vim can do this with "-c" switch if iirc, but I believe there is a more elegant command out there...
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #2   (View Single Post)  
Old 22nd September 2008
anomie's Avatar
anomie anomie is offline
Local
 
Join Date: Apr 2008
Location: Texas
Posts: 446
Thanked 69 Times in 46 Posts
Default

If I'm understanding your quandary: you can replace values in files "in place" with sed. See the manpages for sed(1) -- the -i option (which can be used with or without making a backup).

--- edit ----

I replied with that info after looking at a FBSD 6.3 box. The 7.0 manpages are slightly different, so you may really need the -I option if you're running 7.
__________________
Kill your t.v.

Last edited by anomie; 22nd September 2008 at 08:46 PM.
Reply With Quote
  #3   (View Single Post)  
Old 22nd September 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Thanked 20 Times in 18 Posts
Default

I knew there must be one

Actually, I tested both 6.3 and 7.0. "-i" or "-I" switch isnt enough, sed needs "-e" (edit) as well to make it work

Is there any catch? I see ppl tend to dump the output to a temp file, rather than edit the file on the fly :S
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #4   (View Single Post)  
Old 22nd September 2008
anomie's Avatar
anomie anomie is offline
Local
 
Join Date: Apr 2008
Location: Texas
Posts: 446
Thanked 69 Times in 46 Posts
Default

Well, the catch is you have no backup. So be careful with that option.
__________________
Kill your t.v.
Reply With Quote
  #5   (View Single Post)  
Old 22nd September 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

A more portable approach then relying on GNU SED / FreeBSD SED; might be to use perl, if you know the language.

Code:
$ perl -i -pe 's/foo/bar/g' file


NB that because -i renames or deletes the original file before creating a new file of the same name, UNIX-style soft and hard links will not be preserved.


see perlre, perlrequick, perlreref, perlretut manual pages; if necessary for more advanced pattern matching notes.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Find command (Linux options?) deadeyes FreeBSD General 11 23rd August 2009 06:07 PM
Creating 2 slices Beastie FreeBSD Installation and Upgrading 7 23rd March 2009 12:22 AM
Running a command with input from a file. bigb89 Programming 4 21st January 2009 06:36 PM
how to enable file/command completion for ksh93? TerryP General software and network 1 31st August 2008 08:23 AM
Find pattern in a file and erase it c0mrade Programming 8 25th June 2008 05:31 PM


All times are GMT. The time now is 04:09 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick