[英]perl -a: How to change column separator?
I want to read the columns in a file where the separator is :
. 我想读取分隔符所在的文件中的列
:
。
I tried it like this (because according to http://www.asciitable.com , the octal representation of the colon is 072): 我试过这样的(因为根据http://www.asciitable.com ,冒号的八进制表示是072):
$ echo "a:b:c" | perl -a -072 -ne 'print "$F[1]\n";'
I want it to print b
, but it doesn't work. 我想要它打印
b
,但它不起作用。
Look at -F
in perlrun: 看看perlrun中的
-F
:
% echo "a:b:c" | perl -a -F: -ne 'print "$F[1]\n";'
b
Note that the value is taken as regular expression, so some delimiters may need some extra escaping: 请注意,该值被视为正则表达式,因此某些分隔符可能需要一些额外的转义:
% echo "a.b.c" | perl -a -F. -ne 'print "$F[1]\n";'
% echo "a.b.c" | perl -a -F\\. -ne 'print "$F[1]\n";'
b
-0
specifies the record (line) separator. -0
指定记录 (行)分隔符。 It was cause Perl to receive three lines: 这是因为Perl收到三行:
>echo a:b:c | perl -072 -nE"say"
a:
b:
c
Since there's no whitespace on any of those lines, $F[1]
would be empty if -a
were to be used. 由于这些行中没有任何空格,如果要使用
-a
, $F[1]
将为空。
-F
specifies the input field separator. -F
指定输入字段分隔符。 This is what you want. 这就是你想要的。
perl -F: -lanE'say $F[1];'
Or if you're stuck with an older Perl: 或者,如果您遇到旧的Perl:
perl -F: -lane'print $F[1];'
Command line options are documented in perlrun . perlrun中记录了命令行选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.