perl one-line

# 直接看图片,更好理解
-a 自动分隔模式,用空格分隔$并保存在@F中,也就是@F=split //, $
-F 指定-a的分隔符
-l 对输入的内容进行自动chomp,对输出的内容自动加换行符
-n 相当于while(<>)
-e 执行命令,也就是脚本
-p 自动循环+输出,也就是while(<>){命令(脚本); print;}

cp /etc/passwd .
perl -pe 's/sshd/SSHD/g' passwd
perl -F':' -alne '/sshd/ && print $F[0]' passwd  | sed 's/sshd/SSHD/g'

grep -v "^$" passwd  | perl -alne 'print $a++." ".$_'
#类似于下面的这个
perl -e 'while(<>){chomp($_);if ($_ =~ /^\S/){print $a++." ".$_."\n"}}' passwd
#或
perl -ne 'print $a++." ".$_ if /\S/' passwd
#perl -e 'while(<>){if ($_ =~ /^\s/){print a."\n"}}' passwd

#输出匹配行的计数
perl -lne '$a++ if /^s/; END {print $a+0}' passwd

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

perl -alne '$t += @F; print $t' passwd
perl -alne '$t += @F; END{print $t}' passwd

perl -alne 'map{ /sshd/ && $t++ } @F; END{print $t}' passwd

perl -ne '/sshd/ && print' passwd

perl -ne 'print if /^\d+/' passwd

#矩阵转置
perl -MData::Dumper -e '@matrix=(["a","b","c","d"],["e","f","g","h"],["i","j","k","l"]);print Dumper (\ @matrix); @transposed=map{$x=$_;[map{$matrix[$_][$x]} 0..$#matrix];} 0..$#{$matrix[0]}; print Dumper(\ @transposed)'
#perl打印单引号
#单个单引号 
    perl -e 'print "'\''"'
#打印多个单引号 
perl -e 'print "'\'' '\''"'
此条目发表在Perl分类目录。将固定链接加入收藏夹。

发表评论

邮箱地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Protected with IP Blacklist CloudIP Blacklist Cloud