[英]extract the last column from a tab separated file
I have the following data in a text file. 我在文本文件中有以下数据。
10993 39750 11002
10993 39751 10995
10993 39752 48981
10993 39750 344417 79600
10985 39750 344417 475879
110010 39750 59816
What unix commands I can use to do something like "SELECT LAST_COLUMN WHERE FIRST_COLUMN = '10993'" then the result would be: 我可以使用哪些unix命令来执行“SELECT LAST_COLUMN WHERE FIRST_COLUMN ='10993'”之类的操作,结果将是:
11002
10995
48981
79600
Don't know about perl
but here is an awk
solution: 不知道
perl
但这是一个awk
解决方案:
awk '$1==10993 {print $NF}' file
11002
10995
48981
79600
Perl has an awkish autosplit mode that allows a simple solution to your problem. Perl有一个awkish autosplit模式 ,可以为您的问题提供简单的解决方案。
-a
-一种
turns on autosplit mode when used with a
-n
or-p
.与
-n
或-p
一起使用时打开自动分裂模式。 An implicit split command to the@F
array is done as the first thing inside the implicit while loop produced by the-n
or-p
.对
@F
数组的隐式拆分命令是作为-n
或-p
生成的隐式while循环中的第一个内容完成的。perl -ane 'print pop(@F), "\\n";'
is equivalent to
相当于
while (<>) { @F = split(' '); print pop(@F), "\\n"; }
An alternate delimiter may be specified using
-F
.可以使用
-F
指定备用分隔符。
Putting it to work in your case looks like 在你的情况下使它看起来像
$ perl -lane 'print $F[-1] if $F[0] == 10993' input
11002
10995
48981
79600
I dont think when you can do using command line you should prefer a script for it. 我不认为当你可以使用命令行时,你应该更喜欢它的脚本。
perl -F -lane 'if($F[0]==10993){print $F[(scalar @F)-1]}' your_file
Tested Below: 测试如下:
> cat temp
10993 39750 11002
10993 39751 10995
10993 39752 48981
10993 39750 344417 79600
10985 39750 344417 475879
110010 39750 59816
> perl -F -lane 'if($F[0]==10993){print $F[(scalar @F)-1]}' temp
11002
10995
48981
79600
许多可能的方法之一是awk:
awk '-F\t' 'if ($1 == "wanted-first-column-value") { print $NF }'
Seeing as you've tagged your question with perl, here are some examples for that: 看到你用perl标记了你的问题,这里有一些例子:
Hardcoded in perl: 在perl中硬编码:
#!/usr/bin/perl
use warnings;
use strict;
open INFILE,"<somefilename";
while (<INFILE>)
{
my @cols = split(/\s+/,$_);
if ($cols[0] eq '10993') { print $cols[-1] . "\n"; }
}
Again using perl, but taking it from STDIN instead, so you can just pipe output to it: 再次使用perl,但是从STDIN取而代之,所以你可以只输出输出到它:
#!/usr/bin/perl
use warnings;
use strict;
while (<>)
{
my @cols = split(/\s+/,$_);
if ($cols[0] eq '10993') { print $cols[-1] . "\n"; }
}
Yet another example in perl, taking filename as the first arguement and the required first field as second arguement: 在perl中的又一个例子,将文件名作为第一个争论,并将所需的第一个字段作为第二个争论:
#!/usr/bin/perl
use warnings;
use strict;
unless ($ARGV[0]) { die "No filename specified\n" }
unless ($ARGV[1]) { die "No required field specified\n" }
unless (-e $ARGV[0]) { die "Can't find file $ARGV{0]\n" }
open INFILE,"<ARGV{0]";
while (<INFILE>)
{
my @cols = split(/\s+/,$_);
if ($cols[0] eq $ARGV[1]) { print $cols[-1] . "\n"; }
}
However, it's probably easier to just use awk: 但是,使用awk可能更容易:
awk '{if ($1 == 10993) {print $NF}}' someFileName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.