Perl - One Liners


perldoc perlrun

How can we print lines of the selected files between the START and END markers, without printing the markers themselves?

perl -ne 'print if s/^START\n$// .. s/^END\n$//' files...

How can we print lines following (and including) a line that matches a pattern?

perl -ne '$i = 1 if /pattern/; print if $i--' files

How can we replace Windows line terminators with Unix line terminators?

perl -i -pe 's/\r\n/\n/g' /usr/share/vim/vim63/ftplugin/pdoc4vim.vim

How can we search filename for all instance of sysread and replace them with read?

perl -p -i -e 's/sysread/read/g' filename

How can we execute a string of perl code from command line?

perl -e 'print "Hello World\n"'

What are the important switches?

-e: indicates that the entire program is provided right there on the command line
-n: add a loop around your -e code.
-i: cause any changes to be written back to the file when done

How can we read a file (input.txt) line by line and extract out matching part of the line match the specified pattern?

perl -ne 'print "$1\n" if /Binary files my_quantros.war(.*?) and production_quantros.war\1 differ/' input.txt
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License