Redian新闻
>
how to print the last 2 columns of a text file?
avatar
how to print the last 2 columns of a text file?# Unix - 噫吁兮,危乎高哉
T*B
1
001 kdi ien iej ikd mei dig aks aaa ki01
002 kdi ien iej ikd mei aks aaa ki02
003 kdi ien iej ikd mei dig aks aaa ki03
004 kdi ien iej dig aks aaa ki04
I need to retrieve the last 2 columns from a file like above. That is, the
output should be
aaa ki01
aaa ki02
aaa ki03
aaa ki04
Any suggestions? Thanks a lot.
avatar
s*e
2
Try this!
#!/usr/bin/awk -f
{ if ( NF > 0 ) print $(NF-1) " " $NF }

【在 T***B 的大作中提到】
: 001 kdi ien iej ikd mei dig aks aaa ki01
: 002 kdi ien iej ikd mei aks aaa ki02
: 003 kdi ien iej ikd mei dig aks aaa ki03
: 004 kdi ien iej dig aks aaa ki04
: I need to retrieve the last 2 columns from a file like above. That is, the
: output should be
: aaa ki01
: aaa ki02
: aaa ki03
: aaa ki04

avatar
i*e
3
perl -nae 'print "@F[-2,-1]\n"' your_file

【在 T***B 的大作中提到】
: 001 kdi ien iej ikd mei dig aks aaa ki01
: 002 kdi ien iej ikd mei aks aaa ki02
: 003 kdi ien iej ikd mei dig aks aaa ki03
: 004 kdi ien iej dig aks aaa ki04
: I need to retrieve the last 2 columns from a file like above. That is, the
: output should be
: aaa ki01
: aaa ki02
: aaa ki03
: aaa ki04

avatar
t*m
4
One more example in awk man page
Example 5: Printing Fields in Reverse Order
The following example is an awk script that can be executed
by an awk -f examplescript style command. It prints fields
in reverse order:
{ for (i = NF; i > 0; --i) print $i }
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。