Chapter 1: Introduction to Perl One-Liners

一行流的由来

perl 一行流

怎样用一行流

Perl之单行命令特技
Perl 命令行一句话代码 翻译+总结

Chapter 1: Introduction to Perl One-Liners

Perl one-liners will make you a shell warrior: what took you minutes (or even hours) to solve will now take you only seconds!

  • 改变行距,

  • 编号线,

  • 执行计算,

  • 转换和替换文字,

  • 删除和打印特定的行,

  • 解析日志,

  • 在原地编辑文件,

  • 计算统计,

  • 执行系统管理任务,

  • 一次更新一堆文件。
    ……

     perl -pi -e 's/you/me/g' file 
    
-p argument ensures that the code is executed on every line of input and
that the line is printed after execution
-i argument ensures that file
is edited in-place
  perl -pi.bak -e 's/you/me/g' file
Now Perl creates a file.bak backup file first and only then changes the
contents of file.
    perl -pi -e 's/you/me/g if /we/' file

The regular expression can be anything. Say you want to execute the substitution only on lines with digits in them. You could use the /\d/ regular expression to match numbers:

    perl -pi -e 's/you/me/g if /\d/' file

How about finding all lines in a file that appear more than once?打印重复出现的行:

    perl -ne 'print if $a{$_}++' file

This one-liner also uses the -n command-line argument to
loop over the input, but unlike -p, it doesn’t print the lines automatically.

How about numbering lines? Super simple! Perl’s $. special variable
maintains the current line number. Just print it together with the line:显示行序号:

   perl -ne 'print "$. $_"' file       

You can do the same thing by using the -p argument and modifying
the $_ variable:同上:

    perl -pe '$_ = "$. $_"' file

You can also combine the previous two one-liners to create one that numbers only the repeated lines:显示重复的行号:

  perl -ne 'print "$. $_" if $a{$_}++'  file

Another thing you can do is sum the numbers in each line using thesum function from the List::Util CPAN module. CPAN (ComprehensivePerl Archive Network; http://www.cpan.org/) is an archive of over 100,000 4 Chapter 1reusable Perl modules. List::Util is one of the modules on CPAN, and itcontains various list utility functions. You don’t need to install this module
because it comes with Perl. (It’s in Perl core.)

  perl -MList::Util=sum -alne 'print sum @F'   file  

How about finding the date 1299 days ago? Try this:

  perl -MPOSIX -le '@t = localtime; $t[3] -= 1299; print scalar localtime mktime @t'   

How about generating an eight-letter password? Here you go: 随机密码生成:

    perl -le 'print map { ("a".."z")[rand 26] } 1..8'

Or suppose you want to find the decimal number that corresponds to an IP address. You can use unpack to find it really quickly:

    perl -le 'print unpack("N", 127.0.0.1)'

What about calculations? Let’s find the sum of the numbers in the first column in a table:列求和

    perl -lane '$sum += $F[0]; END { print $sum }'

Now let’s find out how many packets have passed through iptables rules:

      iptables -L -nvx | perl -lane '$pkts += $F[0]; END { print $pkts }' 

How about getting a list of all users on the system?

    perl -a -F: -lne 'print $F[4]' /etc/passwd

Perl one-liners let you accomplish many tasks quickly. You’ll find over 130 one-liners in this book. Read them, try them, and soon enough you’ll be the local shell wizard. ( Just don’t tell your friends—unless you want competition.)
Enjoy!

    原文作者:周运来就是我
    原文地址: https://www.jianshu.com/p/9557c71dd643
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞