find & sed -i work fine. Now need -i workaround for old OS.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find & sed -i work fine. Now need -i workaround for old OS.
# 1  
Old 11-18-2005
CPU & Memory 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  
Old 11-19-2005
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  
Old 11-20-2005
$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  
Old 11-20-2005
Wow, I never new it was so easy to get the PID number from within a script!

Thanks :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

2. UNIX for Advanced & Expert Users

Problem piping find output to awk, 1st line filename is truncated, other lines are fine.

Today I needed to take a look through a load of large backup files, so I wrote the following line to find them, order them by size, and print the file sizes in GB along with the filename. What happened was odd, the output was all as expected except for the first output line which had the filename... (4 Replies)
Discussion started by: gencon
4 Replies

3. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

4. Shell Programming and Scripting

Sed script not working properly on Solaris (works fine on AIX)?

Hi, I have a problem with a SED script that works fine on AIX but does not work properly on a Solaris system. The ksh script executes the SED and puts the output in HTML in tables. But the layout of the output in HTML is not shown correctly(no tables, no color). Can anyone tell if there is... (7 Replies)
Discussion started by: Faith111
7 Replies

5. Red Hat

/usr/bin/find && -exec /bin/rm never work as expected

hi there, Would you able to advise that why the syntax or statement below couldn't work as expected ? /usr/bin/find /backup -name "*tar*" -mtime +2 -exec /bin/rm -f {} \; 1> /dev/null 2>&1 In fact, I was initially located it as in crontab job, but it doesn't work at all. So, I was... (9 Replies)
Discussion started by: rauphelhunter
9 Replies

6. Shell Programming and Scripting

Find & Replace string in multiple files & folders using perl

find . -type f -name "*.sql" -print|xargs perl -i -pe 's/pattern/replaced/g' this is simple logic to find and replace in multiple files & folders Hope this helps. Thanks Zaheer (0 Replies)
Discussion started by: Zaheer.mic
0 Replies

7. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

8. UNIX for Dummies Questions & Answers

sleep 10 && command doesn't work

Hi there, I found a trick to easily postpone a command by a few seconds: supernova:~# sleep 10 && command &If you logout, the command should still be executed... But not all the time. Could anyone of you explain me why the following command is executed even after logging out: supernova:~# sleep... (2 Replies)
Discussion started by: chebarbudo
2 Replies

9. UNIX for Advanced & Expert Users

How csf &apf work ?

I would like to more about how csf &apf firewalls work ? how they use iptables right ? why we need to use them when we got iptables ?:confused: (0 Replies)
Discussion started by: nitin09
0 Replies

10. UNIX for Dummies Questions & Answers

Loop till you find a string in a fine <-- Need Help New to Unix Scripting

Guys - I am new to Unix scripting and am in need for a script that does the following. I have bits and pieces created and tested but i am just having a little difficult time getting it all together. - Loop through till it finds a string in a specific file. Any help is greatly appreciated. ... (1 Reply)
Discussion started by: mrehman
1 Replies
Login or Register to Ask a Question