本文介绍了每个Perl黑客应该知道perl -ne是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来,我一直使用Perl命令行与 -ne 选项,主要是处理文本文件的方式,sed不能。示例:

  cat in.txt | perl -nes / abc / def /; s / fgh / hij /; print; > out.txt 

我不知道从哪里学到这个,今天只读了,发现还有其他形式( perl -pe

我还应该知道 perl -ne

解决方案

perl -ne'CODE'等同于程序

  while(<>){
CODE
}
pre>

perl -ane'CODE' perl -F / PATTERN / 也是很好的成语要知道。它们等同于

  while(<>){
@F = split / \s + $ _;
CODE
}

  while(<>){
@F = split / PATTERN /,$ _
CODE
}

示例: advanced grep

  perl -ne'print if / REGEX1 /&&!/ REGEX2 /& || / REGEX4 /&&!/ REGEX5 /)'输入

perl -F /,/-如果$ F [2] = 4& $ F ] ge2009-07-01&& $ F [3] lt2009-08-01'file.csv




使用不匹配大括号的一个特别聪明的例子是。


I have been using the Perl command line with a -ne option for years, largely to process text files in ways that sed can't. Example:

cat in.txt | perl -ne "s/abc/def/; s/fgh/hij/; print;" > out.txt

I have no idea where I learned this, and have only today read perlrun and found there are other forms (perl -pe for example).

What else should I know about perl -ne?

解决方案

perl -ne 'CODE' is equivalent to the program

while (<>) {
    CODE
}

perl -ane 'CODE' and perl -F/PATTERN/ -ane are also good idioms to know about. They are equivalent to

while (<>) {
    @F = split /\s+/, $_;
    CODE
}

and

while (<>) {
    @F = split /PATTERN/, $_;
    CODE
}

Example: advanced grep:

perl -ne 'print if/REGEX1/&&!/REGEX2/&&(/REGEX3/||/REGEX4/&&!/REGEX5/)' input

perl -F/,/ -ane 'print if $F[2]==4&&$F[3]ge"2009-07-01"&&$F[3]lt"2009-08-01"' file.csv


A particularly clever example that uses mismatched braces is here.

这篇关于每个Perl黑客应该知道perl -ne是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:21