The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Loop till you find a string in a fine <-- Need Help New to Unix Scripting mrehman UNIX for Dummies Questions & Answers 1 01-30-2008 03:01 PM
To Know this find and delete will work ???.. arunkumar_mca UNIX for Dummies Questions & Answers 4 12-27-2007 04:18 AM
workaround for slow du output?? jeepxtreme Shell Programming and Scripting 2 10-30-2007 10:28 AM
find options don't work in script mattd Shell Programming and Scripting 3 04-30-2007 07:31 AM
does find command always work ?! chocolate AIX 1 12-29-2004 01:41 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-17-2005
Registered User
 

Join Date: Nov 2005
Location: London
Posts: 14
Arrow find & sed -i work fine. Now need -i workaround for old OS.

I have a script that does a search and replace on a tree using find, xargs and sed that looks something like this.

find . -type f -print0 | xargs -0 sed -i 's/fromthis/tothis/g'

Now this works fine on new versions on Linux but I need to make the script work on an old RAQ550 that has an older version of Gnu sed that doesn't support the "-i" option in sed that changes files in-place instead of writing to standard output.

Does anyone know any simple, neat workarounds to achieve the same effect without "-i"?

The command above is a simplified version of what I have so simply not using "find" is not the solution I am looking for. Also the script has to handle file names with spaces hense the "xargs -0" stuff.
Reply With Quote
Forum Sponsor
  #2  
Old 11-19-2005
Registered User
 

Join Date: Nov 2005
Location: London
Posts: 14
OK I finally resolved this myself so I am posting the solution in case it helps anyone else out there.

It is possible to use the -i switch of xargs to pass the same file name to several commands quoted in a list. This command should be safe with filenames containing
spaces, which is more than can be said for most shell script examples I've seen on the subject! This is why all the file name references are quoted. Because the whole line is itself quoted, the quotes within have to be escaped with the backslash.

tmpFile="/tmp/sedswap$RANDOM"; find . -type f -print0 | xargs -0 -i bash -c "sed 's/fromthis/tothis/g' \"{}\" > \"$tmpFile\"; rm -f \"{}\"; mv \"$tmpFile\" \"{}\";"

The random temporary file name is just to greatly reduce the risk of any clashes if two instances of this command run concurrently.
Reply With Quote
  #3  
Old 11-19-2005
Registered User
 

Join Date: Jul 2005
Location: Oak Park, IL
Posts: 100
$RANDOM is helpful, but there's still a 1/32000 chance that you'll have a clash within the next instant. You can also try using $$, because that is the PID, which is always increasing (up to 64k) until it restarts. But it will be a while before it cycles around, so you're (often) pretty safe using that.

Sendmail, for example, does even more elaborate filename generation for the files in its mailqueue because creating thousands of files in a short period if time is pretty easy for it. So you won't see it using such a simple algorithm. But it works for many shell script situations.
-Mike
Reply With Quote
  #4  
Old 11-19-2005
Registered User
 

Join Date: Nov 2005
Location: London
Posts: 14
Wow, I never new it was so easy to get the PID number from within a script!

Thanks :-)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0