Thursday, August 10, 2006

Linux : find files, find patterns and replace in one go

Following command finds all text file in current working directory and replaces XYZ with ABC in all found files.

find . -name "*.txt" -exec perl -pe 's/XYZ/ABC/g' -i {} \;

Notes
  • You need to have perl installed on your system
  • with -exec you can execute a command on the found files
  • -i option of perl is used to update files
  • -p option of perl command assumes a loop around the code just like the -n switch. Only difference is that it prints the lines.
  • XYZ is a regular expression to be searched and ABC is the replacement text.

No comments: