2011年2月22日 星期二

grep + sed + xages批次修改檔案內容

egrep -lRZ "\.jpg|\.png|\.gif" . \ 
    | xargs -0 -l sed -i -e 's/\.jpg\|\.gif\|\.png/.bmp/g' 
  • egrep: find matching lines using extended regular expressions
  • -l: only list matching filenames
  • -R: search recursively through all given directories
  • -Z: use \0 as record separator
  • "\.jpg|\.png|\.gif": match one of the strings ".jpg", ".gif" or ".png"
  • .: start the search in the current directory
  • xargs: execute a command with the stdin as argument
  • -0: use \0 as record separator. This is important to match the -Z of egrep and to avoid being fooled by spaces and newlines in input filenames.
  • -l: use one line per command as parameter
  • sed: the stream editor
  • -i: replace the input file with the output without making a backup
  • -e: use the following argument as expression
  • 's/\.jpg\|\.gif\|\.png/.bmp/g': replace all occurrences of the strings ".jpg", ".gif" or ".png" with ".bmp"
reference : http://stackoverflow.com/questions/1169927/using-sed-and-grep-to-search-and-replace

沒有留言:

張貼留言