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 -e 'print "'\''"'
perl -e 'print "'\'' '\''"'


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

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](

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

Here is some inline `code`.

For more help see

Protected with IP Blacklist CloudIP Blacklist Cloud