Find the answer to your Linux question:
Results 1 to 3 of 3
I have about 7 '*.cgi files' in /cgi directory and 1 insert.pl file in /bin . All these files have one line of code which is : PHP Code: use  ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Dec 2009
    Posts
    105

    Using 'sed'


    I have about 7 '*.cgi files' in /cgi directory and 1 insert.pl file in /bin . All these files have one line of code which is :

    PHP Code:
    use bin = /path/to/directory 
    I would like to replace the use bin path to another path in all those files.

    Is this line of code correct to do so :

    Code:
    find /cgi -name "*.cgi" -exec sed -i "%s,/old/path,/new/path,g" '{}' \;
    How do I execute this command for both the *.cgi files in /cgi and insert.pl in /bin at one time?

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Location
    India
    Posts
    2
    Something like this:

    Code:
    $ cat work/test1/a.cgi 
    use bin = /path/to/directory 
    Some other lines
    
    $ cat work/test1/b.cgi 
    use bin = /path/to/directory 
    Some other lines
    
    $ cat work/dir2/c.pl   
    use bin = /path/to/directory 
    This is a perl file
    
    $ find work/test1/ work/dir2/ -type f \( -iname "*.cgi" -or -iname "*.pl" \)
    work/test1/b.cgi
    work/test1/a.cgi
    work/dir2/c.pl
    
    $ for fl in $(find work/test1/ work/dir2/ -type f \( -iname "*.cgi" -or -iname "*.pl" \)); do echo $fl; sed '/use bin/ s!/path/to/directory!/path/to/directory1!g' $fl > $fl.bak ; mv -v $fl.bak $fl ;   done
    
    work/test1/b.cgi
    `work/test1/b.cgi.bak' -> `work/test1/b.cgi'
    work/test1/a.cgi
    `work/test1/a.cgi.bak' -> `work/test1/a.cgi'
    work/dir2/c.pl
    `work/dir2/c.pl.bak' -> `work/dir2/c.pl'
    
    $ cat work/test1/a.cgi 
    use bin = /path/to/directory1 
    Some other lines
    
    $ cat work/test1/b.cgi 
    use bin = /path/to/directory1 
    Some other lines
    
    $ cat work/dir2/c.pl   
    use bin = /path/to/directory1 
    This is a perl file
    Regards,
    Jadu

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    20
    What about this one?
    Code:
    (find /cgi -name "*.cgi"; find /bin -name "insert.pl") | while read -r; do
        sed -ri "s,^(use bin =) /old/path$,\1 /new/path,g" "$REPLY"
    done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •