The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 06:01 PM
To Know this find and delete will work ???.. arunkumar_mca UNIX for Dummies Questions & Answers 4 12-27-2007 07:18 AM
workaround for slow du output?? jeepxtreme Shell Programming and Scripting 2 10-30-2007 01:28 PM
find options don't work in script mattd Shell Programming and Scripting 3 04-30-2007 10:31 AM
does find command always work ?! chocolate AIX 1 12-29-2004 04:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-18-2005
simonb simonb is offline
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.
  #2 (permalink)  
Old 11-19-2005
simonb simonb is offline
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.
  #3 (permalink)  
Old 11-20-2005
mschwage mschwage is offline
Registered User
  
 

Join Date: Jul 2005
Location: Oak Park, IL
Posts: 102
$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
  #4 (permalink)  
Old 11-20-2005
simonb simonb is offline
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 :-)
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0