Find the answer to your Linux question:
Results 1 to 5 of 5
I know how to do grep, that's very nice. But I want to search a folder and it's subfolders and REPLACE a line of code in a file, e.g. this ...
  1. #1
    Just Joined!
    Join Date
    Apr 2003
    Posts
    52

    how to replace a line of code something like grepreplace maybe??

    I know how to do grep, that's very nice.

    But I want to search a folder and it's subfolders and REPLACE a line of code in a file, e.g.

    this line

    if($qr['count']>1){

    with this one

    if($qr['count']>1 && !$usemod_settings['allowDuplicateEmails']){



    How do you do that in linux?
    Thanks,
    Samuel

  2. #2
    Just Joined!
    Join Date
    Nov 2007
    Location
    Germany
    Posts
    37
    You can do that with sed.

    The syntax shouldbe something like
    sed s/pattern/itsreplace/[Mode] file
    Look in the manpage for the details, since i didn't used that before...

    Ogion

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Location
    Redding, CA
    Posts
    20
    Use sed >= 3.95 so you can edit the files directly with -i switch.

    IE -

    sed -i 's?old string?new string?g' /path/to/file

    if you have a whole directory - somethiling like

    find /path/to/directory -type f -print |while read file; do
    sed -i 's?old string?new string?g' "${file}"
    done

    would work.
    Of course back up the directory first.

    I always use a ? instead of / as a delimiter so I don't have to escape the / character in the expression.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    if you know the file names
    Code:
    sed -i 's/cat/dog/' file*.sh
    for one directory level
    Code:
    sed -i 's/cat/dog/' */file*.sh

  5. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by FunkyRes View Post
    I always use a ? instead of / as a delimiter so I don't have to escape the / character in the expression.
    I usually use the pipe symbol (|) since that looks (to me) a more natural separator than a question mark, although pretty much any appropriate punctuation character can be used.

Posting Permissions

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