SED: Converting 5 line script into 1 line script

SED: Converting 5 line script into 1 line script

Post by li.. » Tue, 28 Jun 2005 04:22:35



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

 
 
 

SED: Converting 5 line script into 1 line script

Post by Stephane CHAZELA » Tue, 28 Jun 2005 04:47:15



Quote:> 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;

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

Quote:> (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.

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

 
 
 

SED: Converting 5 line script into 1 line script

Post by William Par » Tue, 28 Jun 2005 04:46:34



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

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

--

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/

 
 
 

SED: Converting 5 line script into 1 line script

Post by li.. » Tue, 28 Jun 2005 06:34:17


Stephane --
Thanks for you reply, I thought they might slightly differnt solution.


> > something like this:
> s/HTTP_\(GET\|POST\|SERVER\|COOKIE\|SESSION\)_VARS/_\1/g

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

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?

Quote:> 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' {} +

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

Thanks again,

David

 
 
 

SED: Converting 5 line script into 1 line script

Post by Bill Seiver » Tue, 28 Jun 2005 07:29:08



> 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

(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

 
 
 

SED: Converting 5 line script into 1 line script

Post by Stephane CHAZELA » Tue, 28 Jun 2005 07:53:33



[...]

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

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

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.

Quote:>> 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' {} +

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

[...]

Yes, something like:

perl -MFile::Find -pi -e '

  s/.../.../g'

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

--
Stphane

 
 
 

SED: Converting 5 line script into 1 line script

Post by li.. » Tue, 28 Jun 2005 20:58:39



> xargs doesn't read files, it reads the filename list from find
> and spawns sed commands with those filenames as arguments to
> sed.

> perl -MFile::Find -pi -e '

>   s/.../.../g'

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

> --
> Stphane

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{

   s/.../.../g;

Thanks again,
David Jackson

 
 
 

SED: Converting 5 line script into 1 line script

Post by Stephane CHAZELA » Wed, 29 Jun 2005 03:08:15



[...]

Quote:> 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?

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

> Would the Perl script look something like this:

> #!/usr/bin/perl -w
> File::Find -pi -e
> BEGIN{

>    s/.../.../g;

Rather something like:

#! /usr/bin/perl -wpi
use File::Find;
BEGIN {
  find(
    sub {

    }, "."
  )

Quote:}

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

 
 
 

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

        a text file comme ca:

homer Simpson, Homer
line two
line three
line four
line five
line six
line seven

marge Simpson, Marge
line two
line three
line four
line five
line six
line seven

------------------------ eof -------------------

problem:
        keep the first line
        delete the next 7 lines (including the blank line)
        repeat

solution:
        sed "n;N;N;N;N;N;N;d" testfile

output:
homer Simpson, Homer        
marge Simpson, Marge        
--
=-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
al aab, ex seders moderator                                   sed u soon
               it is not zat we do not see the  s o l u t i o n          
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-+

2. HP 1100A

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

4. redirecting output of commands

5. read a file line by line in csh script

6. UNIX shell differences and how to change your shell (Monthly Posting)

7. Reading a script file line by line.

8. USR 56k red hat 5.1 setup

9. Is there a way to step thru ksh scripts line by line ?

10. How do I read a file line by line in a csh script?

11. # How to read a textfile line by line using csh shell script ?

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

13. Script for comparing 2 files line by line