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

后端 未结 5 543
北恋
北恋 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: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.

提交回复
热议问题