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 ---
s/HTTP_GET_VARS/_GET/g
s/HTTP_POST_VARS/_POST/g
s/HTTP_SERVER_VARS/_SERVER/g
s/HTTP_COOKIE_VARS/_COOKIE/g
s/HTTP_SESSION_VARS/_SESSION/g
------- 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:
s/HTTP_(GET|POST|SERVER|COOKIE|SESSION)_VARS/_(\2)/g
Were "\2" is GET, POST, etc.

TIA,
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), li...@lastisfirst.org:

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

s/HTTP_\(GET\|POST\|SERVER\|COOKIE\|SESSION\)_VARS/_\1/g

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
files.

--
Stphane



 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 <opengeome...@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
           http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
          http://freshmeat.net/projects/bashdiff/



 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,

David



 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.
(2) "s/HTTP_\(GET|POST|SERVER|COOKIE|SESSION\)_VARS/\1/g"

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

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

-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), li...@lastisfirst.org:
[...]

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}, "."}
  s/.../.../g'

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

--
Stphane



 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
fine?

Would the Perl script look something like this:

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

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), li...@lastisfirst.org:
[...]

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

Rather something like:

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

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.

--
Stphane



 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