четверг, 25 июня 2015 г.

How to change pattern recursively

ack -l 'pattern' | xargs perl -pi -E 's/pattern/replacement/g'

Explanation

ack

ack is an awesome command line tool that is a mix of grepfind, and full Perl regular expressions (not just the GNU subset). Its written in pure Perl, it's fast, it has match highlighting, works on Windows and it's friendlier to programmers than the traditional command line tools. Install it on Ubuntu with sudo apt-get install ack-grep.

xargs

Xargs is an old unix command line tool. It reads items from standard input and executes the command specified followed by the items read for standard input. So basically the list of files generated by ack are being appended to the end of the perl -pi -E 's/pattern/replacemnt/g' command.

perl -pi

Perl is a programming language. The -p option causes Perl to create a loop around your program which iterates over filename arguments. The -i option causes Perl to edit the file in place. You can modify this to create backups. The -E option causes Perl to execute the one line of code specified as the program. In our case the program is just a Perl regex substitution. For more information on Perl command line options perldoc perlrun. For more information on Perl see http://www.perl.org/.

Комментариев нет: