One liners print lines matching a pattern exactly 'n' times. Use ls -l to get the list of files you want to sum and pipe the result to a perl one-liner that sums the fifth column of every line it processes. If you want to insert the same line in multiple files, use the following: perl -pi -le 'print "New line added!!" if $. == 100; close ARGV if eof'
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
116 views
Perl One Liner
One liners print lines matching a pattern exactly 'n' times. Use ls -l to get the list of files you want to sum and pipe the result to a perl one-liner that sums the fifth column of every line it processes. If you want to insert the same line in multiple files, use the following: perl -pi -le 'print "New line added!!" if $. == 100; close ARGV if eof'
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5
Perl HowTo
Your source for Perl tips, howto's, faq and
tutorials Home | Links | RSS feed | Forums Search Topics installation language programming usage command line parameters getting help one liners Get site updates Follow Follow @perlhowto_com @perlhowto_com Navigation forums recent posts User login Username: * Password: * Log in Create new account Request new password Home usage one liners print lines matching a pattern exactly 'n' times ( categories: one liners | regular expressions ) The key is to do the match with the 'g' modifier in list context, then compare the result in scalar context to obtain the number of matches. Example: Print the lines in 'file.txt' that have the string 'for' repeated exactly 3 times: perl -ne 'print if ( ( () = /for/g ) == 3 )' file.txt login or register to post comments calculate the total size of a list of files ( categories: one liners | working with files ) Use ls -l to get the list of files you want to sum and pipe the result to a perl one-liner that sums the fifth column of every line it processes. For example, to get the total size of all your one liners | Perl HowTo http://www.perlhowto.com/one_liners 1 of 5 04/07/2014 05:51 PM Google Search .rpm files in your current directory, use the following: ls -l *rpm | perl -lane '$total += $F[4]; END { print "Total: $total bytes\n" }' login or register to post comments add a line to a file ( categories: one liners ) For example, insert the line "New line added!!" in line 100 of example.txt: perl -pi -le 'print "New line added!!" if $. == 100' example.txt If you want to insert the same line in multiple files, use the following: perl -pi -le 'print "New line added!!" if $. == 100; close ARGV if eof' *.txt ('close ARGV if eof' is needed to reset the variable '$.' before processing the next file). login or register to post comments print a range of lines from a file ( categories: one liners ) Let's say you want to print from 'file.txt' only one liners | Perl HowTo http://www.perlhowto.com/one_liners 2 of 5 04/07/2014 05:51 PM the lines 10 to 20: perl -ne 'print if 10..20' file.txt login or register to post comments check the syntax of a perl script ( categories: one liners ) perl -wc script.pl NOTES: - The above command just check the syntax, it does not execute the script. - If you don't want to see the warnings, remove 'w' from the command line arguments. login or register to post comments remove all blank lines of a file ( categories: one liners ) Without saving a backup of the previous file: perl -ni -e 'print unless /^$/' file Saving the original file as file.bak: perl -ni.bak -e 'print unless /^$/' file login or register to post comments one liners | Perl HowTo http://www.perlhowto.com/one_liners 3 of 5 04/07/2014 05:51 PM replace all ocurrences of 'foo' with 'bar' in a file ( categories: one liners ) Without saving a backup of the previous file: perl -pi -e 's/foo/bar/g' file Saving the original file as file.bak: perl -pi.bak -e 's/foo /bar/g' file login or register to post comments check the version of installed modules ( categories: one liners | perl modules ) To check the version number of a module use the following one-liner: 'perl -M -e 'print "$<module>::VERSION \n"'. Example: #-- check the version number of CGI module perl -MCGI -e 'print "$CGI::VERSION \n"' login or register to post comments list the directories where perl modules are one liners | Perl HowTo http://www.perlhowto.com/one_liners 4 of 5 04/07/2014 05:51 PM Home | Links | RSS feed | Forums Copyright 2006 Toshiro Viera Stalker - All rights reserved located ( categories: one liners | perl modules ) The array @INC contains the list of places that the 'do EXPR', 'require', or 'use' constructs look for their library files. The following one-liner shows the contents of that array: perl -e 'foreach $folder (@INC) { print "$folder\n";}' login or register to post comments login or register to post comments one liners | Perl HowTo http://www.perlhowto.com/one_liners 5 of 5 04/07/2014 05:51 PM