It is currently Thu, 20 Jan 2022 05:24:23 GMT

Author Message
 SED: Converting 5 line script into 1 line script
Howdy --
I am doing,  a global search and replace in for a entire directory
tree. The command I'm using (which works, but it' not as much fun) is:

find . -name "*.php" -exec sed -i -f script_script

----- script_script ---
------- end of script --

Is there some why I can:
(1) do a recursive search and replace with sed;
(2) use some kind of sed regrex and combine 5 lines into 1.

something like this:
Were "\2" is GET, POST, etc.

David Jackson

 Fri, 14 Dec 2007 03:22:35 GMT   
 SED: Converting 5 line script into 1 line script
2005-06-26, 12:22(-07),

No, sed is not a directory tree searcher, just a stream editor.
find is the tree searcher, one job for every one.


find . -name "*.php" -type f -print0 |
  xargs -r0 sed -i 's/...'

Or POSIXly with systems that have perl (most nowadays):

find . -name '*.php' -type f -exec perl -pi -e '
  s/HTTP_(GET|POST|...)_VARS/_$1/g' {} +

With zsh, you could do:

sed -i 's...' ./**/*.php(.D)

(use zargs if you get a "Arg list too long" error).

"." is the equivalent of find's "-type f". "D" is to include dot


 Fri, 14 Dec 2007 03:47:15 GMT   
 SED: Converting 5 line script into 1 line script

I don't understand your question.  You seem to have the answer already.
Perhaps, you are looking for different approach, like
    find ... | xargs sed ...

William Park <>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
BashDiff: Super Bash shell

 Fri, 14 Dec 2007 03:46:34 GMT   
 SED: Converting 5 line script into 1 line script
Stephane --
Thanks for you reply, I thought they might slightly differnt solution.

I forgot about the {} at the end of the exec statement, xargs loads a
buffer with files instead of reading one file at a time?

This is good also, any idea how how could File module and do the whole
job with in Perl?

Thanks again,


 Fri, 14 Dec 2007 05:34:17 GMT   
 SED: Converting 5 line script into 1 line script

(1) Can't be done with sed.  Use find.

 > find . -name "*.php" -exec sed -i -f script_script
Missing {}

Could do something like:
   find . -name "*.php" -type f -print | \
       while read fn
           sed "s/HTTP_\(GET|POST|SERVER|COOKIE|SESSION\)_VARS/_\1/g" \
               "$fn" > "$nfn"
           if [ $? -eq 0 ] ; then
               mv "$nfn" "$fn"

-type f included so you don't sed a directory named anything.php

Bill Seivert

 Fri, 14 Dec 2007 06:29:08 GMT   
 SED: Converting 5 line script into 1 line script
2005-06-26, 14:34(-07),

xargs doesn't read files, it reads the filename list from find
and spawns sed commands with those filenames as arguments to
sed. It means that sed is called with several files at a time.
That means that fewer seds are called. It works like -exec cmd
{} + (note the "+" instead of ";") except that you benefit from
find and xargs+sed running in parallel.


Yes, something like:

perl -MFile::Find -pi -e '
  BEGIN{find sub{push @ARGV, $File::Find::name if /\.php$/ && -f}, "."}

Best would be to do the substitution in the sub, but then you'd
have to write it in full.


 Fri, 14 Dec 2007 06:53:33 GMT   
 SED: Converting 5 line script into 1 line script

Stephane --
Thanks again for the script ideas.
Running the find + xargs command results in the following error:
xargs:sed:terminated by signal. I wondering if this could be a memory
problem, since I've tested it on a smaller file system, and it works

Would the Perl script look something like this:

#!/usr/bin/perl -w
File::Find -pi -e
find sub{push @ARGV, $File::Find::name if /\.php$/ && -f}, "."}

Thanks again,
David Jackson

 Fri, 14 Dec 2007 19:58:39 GMT   
 SED: Converting 5 line script into 1 line script
2005-06-27, 04:58(-07),

No idea why you get that. Are you piping xargs sed into another

Rather something like:

#! /usr/bin/perl -wpi
use File::Find;
    sub {
      push @ARGV, $File::Find::name if /\.php$/ && -f
    }, "."

But note that that means that the whole list is built and stored
in memory and processed at the end. You might want to have
find() run the substitution in its sub function.


 Sat, 15 Dec 2007 02:08:15 GMT   
   [ 8 post ] 

Similar Threads

1. . sed script: keep 1 line, delete next 7 lines

2. "new line" in sed script

3. how to add new lines in sed script

4. insert empty lines with sed (shell script)

5. sed script different than command line

6. a SED script to find length of the longest line in a file

7. Reading a script file line by line.

8. read a file line by line in csh script

9. please help how to join 3 lines into one line via script

10. unix shell script to access a file line by line

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software