Best way to do a find/replace in several files?

后端 未结 5 542
北恋
北恋 2021-01-30 01:56

what\'s the best way to do this? I\'m no command line warrior, but I was thinking there\'s possibly a way of using grep and cat.

I just want to

相关标签:
5条回答
  • 2021-01-30 02:03

    An alternative to sed is using rpl (e.g. available from http://rpl.sourceforge.net/ or your GNU/Linux distribution), like rpl --recursive --verbose --whole-words 'F' 'A' grades/

    0 讨论(0)
  • 2021-01-30 02:11

    As Paul said, you want to first find the files you want to edit and then edit them. An alternative to using find is to use GNU grep (the default on Ubuntu), e.g.:

    grep -r -l from . | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

    You can also use ack-grep (sudo apt-get install ack-grep or visit http://petdance.com/ack/) as well, if you know you only want a certain type of file, and want to ignore things in version control directories. e.g., if you only want text files,

    ack -l --print0 --text from | xargs -0 -n 1 sed -i -e 's/from/to/g'
    # `from` here is an arbitrary commonly occurring keyword
    

    An alternative to using sed is to use perl which can process multiple files per command, e.g.,

    grep -r -l from . | xargs perl -pi.bak -e 's/from/to/g'
    

    Here, perl is told to edit in place, making a .bak file first.

    You can combine any of the left-hand sides of the pipe with the right-hand sides, depending on your preference.

    0 讨论(0)
  • 2021-01-30 02:16

    I'll throw in another example for folks using ag, The Silver Searcher to do find/replace operations on multiple files.

    Complete example:

    ag -l "search string" | xargs sed -i '' -e 's/from/to/g'
    

    If we break this down, what we get is:

    # returns a list of files containing matching string
    ag -l "search string"
    

    Next, we have:

    # consume the list of piped files and prepare to run foregoing command
    # for each file delimited by newline
    xargs
    

    Finally, the string replacement command:

    # -i '' means edit files in place and the '' means do not create a backup
    # -e 's/from/to/g' specifies the command to run, in this case,
    # global, search and replace
    
    sed -i '' -e 's/from/to/g'
    
    0 讨论(0)
  • 2021-01-30 02:29
    find . -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

    The first part of that is a find command to find the files you want to change. You may need to modify that appropriately. The xargs command takes every file the find found and applies the sed command to it. The sed command takes every instance of from and replaces it with to. That's a standard regular expression, so modify it as you need.

    If you are using svn beware. Your .svn-directories will be search and replaced as well. You have to exclude those, e.g., like this:

    find . ! -regex ".*[/]\.svn[/]?.*" -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    

    or

    find . -name .svn -prune -o -type f -print0 | xargs -0 -n 1 sed -i -e 's/from/to/g'
    
    0 讨论(0)
  • 2021-01-30 02:29
    comment() { 
    }
    doc() { 
    }
    function agr { 
    doc 'usage: from=sth to=another agr [ag-args]'
    comment -l --files-with-matches
    
    ag -0 -l "$from" "${@}" | pre-files "$from" "$to"
    }
    pre-files() {
    doc 'stdin should be null-separated list of files that need replacement; $1 the string to replace, $2 the replacement.'
    comment '-i backs up original input files with the supplied extension (leave empty for no backup; needed for in-place replacement.)(do not put whitespace between -i and its arg.)'
    comment '-r, --no-run-if-empty
                  If  the  standard input does not contain any nonblanks,
                  do not run the command.  Normally, the command  is  run
                  once  even  if there is no input.  This option is a GNU
                  extension.'
    
    AGR_FROM="$1" AGR_TO="$2" xargs -r0 perl -pi.pbak -e 's/$ENV{AGR_FROM}/$ENV{AGR_TO}/g'
    }
    

    You can use it like this:

    from=str1 to=sth agr path1 path2 ...
    

    Supply no paths to make it use the current directory. Note that ag, xargs, and perl need to be installed and on PATH.

    0 讨论(0)
提交回复
热议问题