Need a unix command - Slackware

This is a discussion on Need a unix command - Slackware ; I have a few hundred PHP files in a directory. I need to convert them from PHP 4 to PHP 5. I'm looking for a command that will read all the files in the directory and replace any occurrence of ...

+ Reply to Thread
Results 1 to 19 of 19

Thread: Need a unix command

  1. Need a unix command

    I have a few hundred PHP files in a directory. I need to convert them
    from PHP 4 to PHP 5. I'm looking for a command that will read all the
    files in the directory and replace any occurrence of "xxxx" in a string
    with "yyyyy". I can grep them, but don't know how to do the replace.

    Can anyone help here?

    Thanks,

    Al

  2. Re: Need a unix command

    In article
    ,
    ANC wrote:

    > I have a few hundred PHP files in a directory. I need to convert them
    > from PHP 4 to PHP 5. I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.
    >
    > Can anyone help here?
    >
    > Thanks,
    >
    > Al


    To answer my own question I found this on the web:

    perl -pi -e 's/old_string/new_string/g' file_pattern

    perl -pi -e 's/xxxxx/yyyyy/g' *.php

    (http://kb.iu.edu/data/afrk.html)

    Can any of you perl experts confirm that this might work? Perl always
    looks like Sanskrit to me!!

    Al

  3. Re: Need a unix command

    On 2007-07-28, ANC wrote:
    > I have a few hundred PHP files in a directory. I need to convert them
    > from PHP 4 to PHP 5. I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.


    I love the perversity of using Perl to modify PHP scripts. Keep
    backups, or read perldoc perlrun about the -i switch.

    perl -pi -e 's/xxxx/yyyyy/g' file1 file2 ...

    --keith

    --
    kkeller-usenet@wombat.san-francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information


  4. Re: Need a unix command

    On Fri, 27 Jul 2007 23:19:16 -0700, ANC wrote:

    > I have a few hundred PHP files in a directory. I need to convert them
    > from PHP 4 to PHP 5. I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.
    >
    > Can anyone help here?


    Do you mean you want to change the names of the files or the
    content of these files?

    if you're after the content, here's another variant:
    # for fff in yourpath; do sed -i s/xxxxx/yyyyy/g ${fff} ; done

    if your question is about changing the names of the files
    then it can be trickier, you'd have to get certain during
    the process of renaming them there wouldn't exist a coincidence
    on duplicate names, and of course depending on your uses in
    "namespace" you'll have to take care of LC_ALL or LANG and
    protect your variables names.



  5. Re: Need a unix command

    On 2007-07-28, ANC wrote:

    > Can anyone help here?


    http://www.eng.cam.ac.uk/help/tpl/unix/sed.html

    nb

  6. Re: Need a unix command

    "ANC" typed:

    > To answer my own question I found this on the web:
    >
    > perl -pi -e 's/old_string/new_string/g' file_pattern
    >
    > perl -pi -e 's/xxxxx/yyyyy/g' *.php
    >
    > (http://kb.iu.edu/data/afrk.html)
    >
    > Can any of you perl experts confirm that this might work? Perl always
    > looks like Sanskrit to me!!


    Yup, that'd work.

    http://ayaz.wordpress.com/2006/12/30...d-commit-back/

    --
    Ayaz Ahmed Khan

    The church is near but the road is icy; the bar is far away but I will
    walk carefully.
    -- Russian Proverb



  7. Re: Need a unix command

    "loki harfagr" typed:

    > if your question is about changing the names of the files
    > then it can be trickier, you'd have to get certain during
    > the process of renaming them there wouldn't exist a coincidence
    > on duplicate names, and of course depending on your uses in
    > "namespace" you'll have to take care of LC_ALL or LANG and
    > protect your variables names.


    Those are things to look after, definitely. Additionally, there is the
    rename(1) utility.

    --
    Ayaz Ahmed Khan

    The church is near but the road is icy; the bar is far away but I will
    walk carefully.
    -- Russian Proverb



  8. Re: Need a unix command

    ANC wrote:

    > I have a few hundred PHP files in a directory. I need to convert them
    > from PHP 4 to PHP 5. I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.


    find . -name '*' -maxdepth 1 -type f > ~/myfiles.txt

    Or, perhaps:

    find . -name '*.php' -maxdepth 1 -type f > ~/myfiles.txt

    And then:

    Call the next (Perl) script like so:

    $ modmyfils ~/myfiles.txt

    Perl script 1 of 2 is next:

    #!/usr/bin/perl -w
    use strict;

    # next line is the name of this script
    # modmyfils
    # reads myfiles.txt
    # runs filmodpl on each file that's listed in myfiles.txt

    my $fil2read = shift;
    open(FILNAMS, "<", $fil2read) or die "cant open $fil2read: $!";
    my @lines = ;
    close(FILNAMS) or die "cant close $fil2read: $!";

    foreach my $line ( @lines ) { # ea. line is a filename
    chomp($line);
    system("filmodpl $line"); # call filmodpl on filename
    }
    # end --------------- end ---------- end --------


    Next is Perl script 2 of 2, the filmodpl script which is called by the
    modmyfils script:

    #!/usr/bin/perl -w
    use strict;

    my $old = shift;
    my $new = "$old.tmp";
    open(OLD, "<", $old) or die "cant open $old: $!";
    open(NEW, ">", $new) or die "cant open $new: $!";
    while () {
    s/xxxxxx/yyyyyyyy/; # line per line basis (does 1 line at a time)
    print NEW;
    }
    close(OLD) or die "cant close $old: $!";
    close(NEW) or die "cant close $new: $!";
    rename($old, "$old.orig") or die "cant rename $old to $old.orig: $!";
    rename($new, "$old") or die "cant rename $new to $old: $!";
    # end ----------

    --
    nalA


  9. Re: Need a unix command

    In article
    ,
    ANC wrote:

    > I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.


    In one line:

    sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)

    - Martijn

  10. Re: Need a unix command

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1
    NotDashEscaped: You need GnuPG to verify this message

    * Martijn Dekker writes:

    > In article
    > ,
    > ANC wrote:


    >> I'm looking for a command that will read all the
    >> files in the directory and replace any occurrence of "xxxx" in a string
    >> with "yyyyy". I can grep them, but don't know how to do the replace.


    > In one line:


    > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)


    That runs the risk of the command line becoming too long if there are
    lots of files. This would be better...

    find YourDirectory -type f -exec sed -i 's/xxxx/yyyyy/g' {} \;

    --
    |---------------------|
    | Genius - Is the ability to reduce |
    | the complicated to the simple |
    |-------------------------------------|
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.3 (GNU/Linux)
    Comment: The SXEmacs Project
    Comment: Eicq - The SXEmacs ICQ Client

    iEYEARECAAYFAkasF1QACgkQHSfbS6lLMAOQ2wCfVM7HwmggZx X9qTjwFtaabKvv
    XWwAn1JS62Zs5SXe8T3uS4aLOkrh4n0C
    =f0Ri
    -----END PGP SIGNATURE-----

  11. Re: Need a unix command

    Martijn Dekker wrote:
    > In article
    > ,
    > ANC wrote:
    >
    >> I'm looking for a command that will read all the
    >> files in the directory and replace any occurrence of "xxxx" in a string
    >> with "yyyyy". I can grep them, but don't know how to do the replace.

    >
    > In one line:
    >
    > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)
    >
    > - Martijn

    OT i have a little question concerning this
    what's the difference between $( cmds ) en `cmds` except the fact
    that $() is less suitable for long pipelines, this is why i prefer the
    backtick command substitution

  12. Re: Need a unix command

    "goarilla" typed:

    > OT i have a little question concerning this
    > what's the difference between $( cmds ) en `cmds` except the fact
    > that $() is less suitable for long pipelines, this is why i prefer the
    > backtick command substitution


    The section `Command Substitution' in bash(1) describes the differences in
    detail. I will quote from there:

    When the old-style backquote form of substitution is used,
    backslash retains its literal meaning except when followed by $, `,
    or \. The first backquote not preceded by a backslash terminates the
    command sub- stitution. When using the $(command) form, all
    characters between the parentheses make up the command; none are
    treated specially.

    Command substitutions may be nested. To nest when using the backquoted
    form, escape the inner backquotes with backslashes.

    If the substitution appears within double quotes, word splitting
    and pathname expansion are not performed on the results.

    --
    Ayaz Ahmed Khan

    The church is near but the road is icy; the bar is far away but I will
    walk carefully.
    -- Russian Proverb



  13. Re: Need a unix command

    Ayaz Ahmed Khan wrote:
    > "goarilla" typed:
    >
    >> OT i have a little question concerning this
    >> what's the difference between $( cmds ) en `cmds` except the fact
    >> that $() is less suitable for long pipelines, this is why i prefer the
    >> backtick command substitution

    >
    > The section `Command Substitution' in bash(1) describes the differences in
    > detail. I will quote from there:
    >
    > When the old-style backquote form of substitution is used,
    > backslash retains its literal meaning except when followed by $, `,
    > or \. The first backquote not preceded by a backslash terminates the
    > command sub- stitution. When using the $(command) form, all
    > characters between the parentheses make up the command; none are
    > treated specially.
    >
    > Command substitutions may be nested. To nest when using the backquoted
    > form, escape the inner backquotes with backslashes.
    >
    > If the substitution appears within double quotes, word splitting
    > and pathname expansion are not performed on the results.
    >

    thx but it's gonna take some time to understand it tho i wished the bash
    manual had more examples

  14. Re: Need a unix command

    In article <46ac2571$0$14237$ba620e4c@news.skynet.be>,
    goarilla <"kevin DOT paulus AT skynet DOT be"> wrote:

    > Martijn Dekker wrote:
    > > In one line:
    > >
    > > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)
    > >

    > OT i have a little question concerning this
    > what's the difference between $( cmds ) en `cmds`


    Essentially none. The former is *far* more readable -- it's just barely
    possible to distinguish backticks from normal single quotes on-screen;
    many mistakes must be made because of this.

    Backticks have also been deprecated by POSIX in favor of the $( ) form,
    a VERY long time ago now.

    Oh, and you can nest the new form without tedious escaping.

    > except the fact
    > that $() is less suitable for long pipelines,


    What makes you think that? AFAIK, they're functionally equivalent.

    - Martijn

  15. Re: Need a unix command

    In article ,
    Steve Youngs wrote:

    > * Martijn Dekker writes:
    >
    > > In one line:

    >
    > > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)

    >
    > That runs the risk of the command line becoming too long if there are
    > lots of files.


    Only if the total list of paths to filenames exceeds 32 kilobytes. That
    hardly seems likely.

    > This would be better...
    >
    > find YourDirectory -type f -exec sed -i 's/xxxx/yyyyy/g' {} \;


    So, since we're picking nits anyway, the -exec option to `find' spawns a
    separate `sed' process for each file, which is needlessly inefficient.
    Using `xargs' is even better, as it'll pass as many arguments to `sed'
    as it will take, and use only as many process launches as it needs:

    find YourDirectory -type f | xargs sed -i 's/xxxx/yyyyy/g'

    or, if the filenames might contain spaces:

    find YourDirectory -type f -print0 | xargs -0 sed -i 's/xxxx/yyyyy/g'

    - Martijn, done nitpicking now

  16. Re: Need a unix command

    On Sun, 29 Jul 2007 13:29:31 +0200, Martijn Dekker wrote:

    > In article ,
    > Steve Youngs wrote:
    >
    >> * Martijn Dekker writes:
    >>
    >> > In one line:

    >>
    >> > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)

    >>
    >> That runs the risk of the command line becoming too long if there are
    >> lots of files.

    >
    > Only if the total list of paths to filenames exceeds 32 kilobytes. That
    > hardly seems likely.


    In a production environment the probability of this
    kind of case is at "least" 1, and can even go higher for
    large values of 1 ;-)

    Now, for the OP case, he said "a few hundred PHP files in a directory",
    if you imagine the files have clever descriptive names instead
    of 8+3 it'd only take an average length of 64 chars per name
    to crash with 512 files :-)

    >> This would be better...
    >>
    >> find YourDirectory -type f -exec sed -i 's/xxxx/yyyyy/g' {} \;

    >
    > So, since we're picking nits anyway, the -exec option to `find' spawns a
    > separate `sed' process for each file, which is needlessly inefficient.
    > Using `xargs' is even better, as it'll pass as many arguments to `sed'
    > as it will take, and use only as many process launches as it needs:
    >
    > find YourDirectory -type f | xargs sed -i 's/xxxx/yyyyy/g'


    Just keeping on find itself your version of find can also
    have the extension of \+, so:
    # find YourDirectory -type f -exec sed -i 's/xxxx/yyyyy/g' {} \+

    would be the 'find' way.
    ------------
    -exec command {} +
    This variant of the -exec option runs the specified command on the selected files, but the command
    line is built by appending each selected file name at the end; the total number of invocations of the
    command will be much less than the number of matched files. The command line is built in much the
    same way that xargs builds its command lines. Only one instance of '{}' is allowed within the com-
    mand. The command is executed in the starting directory.
    ------------

    > or, if the filenames might contain spaces:
    >
    > find YourDirectory -type f -print0 | xargs -0 sed -i 's/xxxx/yyyyy/g'



    Now, let's crash some dummies to resume the different methods :-)

    ------------
    $ mkdir /tmp/CRASHTEST
    $ cd /tmp/CRASHTEST
    $ for i in $(seq 40000); do date +%s > $i; done
    ------------

    test 1, the unprotected glob, wam blam, ouch:
    ------------
    $ time sed 's/1/Y/g' $(find /tmp/CRASHTEST/ -type f) 1> /dev/null
    -bash: /usr/bin/sed: Argument list too long

    real 0m0.261s
    user 0m0.184s
    sys 0m0.096s
    ------------

    test 2 and 3, pure shell methods, 'for' and 'while read',
    correct but consuming:
    ------------
    $ time for fff in /tmp/CRASHTEST/*; do sed s/1/Y/g "${fff}" 1> /dev/null ; done

    real 1m55.026s
    user 0m24.215s
    sys 1m30.121s

    $ time ls /tmp/CRASHTEST/ | while read fff; do sed s/1/Y/g "${fff}" 1> /dev/null ; done

    real 2m0.306s
    user 0m47.498s
    sys 1m29.972s
    ------------

    test 4, pure find exec, correct but still consuming:
    ------------
    $ time find /tmp/CRASHTEST/ -type f -exec sed 's/1/Y/g' {} \; 1> /dev/null

    real 0m58.589s
    user 0m16.310s
    sys 0m40.729s
    ------------

    test 5, find exec and xargs, woof:
    ------------
    $ time find /tmp/CRASHTEST/ -type f | xargs sed 's/1/Y/g' 1> /dev/null

    real 0m1.071s
    user 0m0.346s
    sys 0m0.795s
    ------------

    test 6, 'grouping' option in find exec, fast eddie:
    ------------
    $ time find /tmp/CRASHTEST/ -type f -exec sed 's/1/Y/g' {} \+ 1> /dev/null

    real 0m1.166s
    user 0m0.315s
    sys 0m0.827s
    ------------

    Then, I'd chose the last one when the + option is available
    and I'd go for the 'while read' (or the 'for') for portability,
    as I suppose Al doesn't care much about portability for the one
    shot case of his directory to upgrade I agree that was just the
    result of having dull weather today ;-)


    > - Martijn, done nitpicking now


    Loki, western Europe nitpicking golfer control second assistant :-)

  17. Re: Need a unix command

    In article ,
    Ayaz Ahmed Khan wrote:

    > "ANC" typed:
    >
    > > To answer my own question I found this on the web:
    > >
    > > perl -pi -e 's/old_string/new_string/g' file_pattern
    > >
    > > perl -pi -e 's/xxxxx/yyyyy/g' *.php
    > >
    > > (http://kb.iu.edu/data/afrk.html)
    > >
    > > Can any of you perl experts confirm that this might work? Perl always
    > > looks like Sanskrit to me!!

    >
    > Yup, that'd work.
    >


    Yes it did. However I had to make a few changes because of my string.

    perl -pi -e 's/displayonly=\$this\-\>mDisplayOnly/displayonly=false/g'
    ~/php-convert/pub456a/*.php

    For some reason it wanted the path to the directory even though I was in
    the directory at the time. Probably a Mac issue.

    Second I had to put in the backslashes. Not sure why a $ sign needs to
    be escaped, but it seems to need it.

    What, you ask was I doing? I'm trying to get our www.jaya123.com
    application ready for PHP 5 and there is one code construct ($this->) we
    used that was fine in 4 but craps out in 5 because of the change in the
    class structure. We also want to change $HTTP_POST_VARS to the
    super-globals $_POST, get, session, etc. With the perl line, it all took
    about 20 minutes to make all the changes to the couple of hundred mostly
    small (under 1000 lines) PHP files we have.

    Thanks. I enjoyed reading the alternatives.

    Al

  18. Re: Need a unix command

    Martijn Dekker wrote:
    > In article ,
    > Steve Youngs wrote:
    >
    >> * Martijn Dekker writes:
    >>
    >> > In one line:

    >>
    >> > sed --in-place 's/xxxx/yyyyy/g' $(find YourDirectory -type f)

    >>
    >> That runs the risk of the command line becoming too long if there are
    >> lots of files.

    >
    > Only if the total list of paths to filenames exceeds 32 kilobytes. That
    > hardly seems likely.


    Allow me to pick a nit too. From /usr/src/linux/include/linux/limits.h:
    #define ARG_MAX 131072 /* # bytes of args + environ for exec() */
    The limit for the total of environment plus command line is 128
    kilobytes. Unless your environment occupies 96 KB you'll have a lot
    more space for command line arguments than 32 KB. Nevertheless there
    _is_ an arbitrary limit (at 128 KB, including the environment) that
    you should be aware of when using the output of 'find' as command line
    arguments for some program. You'll likely hit this limit if you start
    'find' in / for example.

    >
    >> This would be better...
    >>
    >> find YourDirectory -type f -exec sed -i 's/xxxx/yyyyy/g' {} \;

    >
    > So, since we're picking nits anyway, the -exec option to `find' spawns a
    > separate `sed' process for each file, which is needlessly inefficient.
    > Using `xargs' is even better, as it'll pass as many arguments to `sed'
    > as it will take, and use only as many process launches as it needs:
    >
    > find YourDirectory -type f | xargs sed -i 's/xxxx/yyyyy/g'
    >
    > or, if the filenames might contain spaces:


    or, if the filenames might contain single quote chars, semicolons,
    brackets or any other junk that is supposed not to be used in file
    names. This is a common problem when you need to process files on a
    windows partition or a 'shared disk' from a windows system.

    > find YourDirectory -type f -print0 | xargs -0 sed -i 's/xxxx/yyyyy/g'


    This "find ... -print0 | xargs -0 ..." combination is my preferred way
    to process unknown directory trees. It will properly and efficiently
    handle otherwise problematic file names and huge amounts of files.
    I think this combination should be the standard response, or at least
    be mentioned as a 'fail proof' alternative, for most questions regarding
    the processing of many files with a single command.

    Regards,

    Kees.

    --
    Kees Theunissen.

  19. Re: Need a unix command

    ANC wrote:
    >
    > I have a few hundred PHP files in a directory. I need to convert them
    > from PHP 4 to PHP 5. I'm looking for a command that will read all the
    > files in the directory and replace any occurrence of "xxxx" in a string
    > with "yyyyy". I can grep them, but don't know how to do the replace.
    >
    > Can anyone help here?


    Try id2id-20.zip, available at:



    Pure portable standard C, with source.

    --



    cbfalconer at maineline dot net


    --
    Posted via a free Usenet account from http://www.teranews.com


+ Reply to Thread