[英]How can I use awk (or cut) to print the last field per line from a pipe separated file?
I have a file like that 我有一个这样的文件
14618 | 184.72.251.121 | 184.72.128.0/17 | US | arin | 2010-01-26 | AMAZON-AES - Amazon.com, Inc., US
14618 | 107.21.223.240 | 107.21.128.0/17 | US | arin | 2011-05-03 | AMAZON-AES - Amazon.com, Inc., US
16509 | 52.85.180.156 | 52.85.180.0/23 | US | arin | | AMAZON-02 - Amazon.com, Inc., US
61337 | 85.199.214.99 | 85.199.212.0/22 | GB | ripencc | 2005-02-08 | ECOM-AS ============================================, GB
31034 | 94.177.187.22 | 94.177.160.0/19 | IT | ripencc | 2008-08-14 | ARUBA-ASN, IT
8816 | 212.45.144.206 | 212.45.128.0/19 | IT | ripencc | | IT-STCOM, IT
I must extract the last field and all its content. 我必须提取最后一个字段及其所有内容。 Notice that sometimes the penultimate column is empty. 请注意,有时倒数第二列为空。
I tried 我试过了
awk 'BEGIN { ORS = " "}; {for(i=13;i<NF;++i) print $i}'
but sometimes it doesn't work or it misses some words. 但有时它不起作用或遗漏了一些单词。
Can you help me please? 你能帮我吗?
Try this - 尝试这个 -
awk -F'|' '{print $NF}' file
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
If this is not your desired output then please post your expected output. 如果这不是您期望的输出,请发布您的期望输出。
With sed: substitute the longest match up to |
使用sed:替换最长的匹配项|
by nothing. 一无所有。
$ sed 's/.*|//' infile
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
If you don't want the leading blanks, remove them as well: 如果您不希望前导空格,也将其删除:
$ sed 's/.*|[[:blank:]]*//' infile
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
This matches up to the last |
这匹配到最后一个|
and includes as many blanks as possible after the |
并在|
之后包含尽可能多的空格|
, then removes the match. ,然后删除匹配项。
还原每一行,以剪切方式读取第一列,然后还原:
rev input | cut -f1 -d\| | rev
It looks like you would be better off using awk
with the |
看起来您最好将awk
与|
character as the field separator ( awk -F'|'
) but, if they're always going to be nicely lined up like that, cut
may well be a better option: 字符作为字段分隔符( awk -F'|'
),但是,如果始终像这样将它们很好地对齐,则cut
可能是一个更好的选择:
cut -c81- inputFile
That would give you all the characters from position 81 onwards in each line, which is roughly right just from me eyeballing the data. 这样一来,您就可以在每行中找到从位置81开始的所有字符,而从我眼中的数据来看,这大概是正确的。 A bit of trial and error would give a more accurate value to use in place of 81
since I may be off by a couple - my eyeballs aren't as young as they once were :-) 试错的位将给出一个更准确的值来代替使用81
,因为我可以用一对夫妇离开-我的眼球都不像年轻时那样他们曾经是:-)
It appears that you just want the 7th |
看来您只想要第七|
-delimited field of your input: 输入的定界字段:
cut -d\| -f7
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
If you want to get rid of the leading space, you can remove that with cut
too: 如果您想摆脱前导空间,也可以用cut
删除它:
cut -d\| -f7- | cut -c2-
I'm assuming that by the last field you mean the last column. 我假设在最后一个字段中,您指的是最后一列。 This is what you need: 这是您需要的:
awk -F'\|' '{print $7}' MyFile
It will print the last column of your file in this case. 在这种情况下,它将打印文件的最后一列。
You can use grep
: 您可以使用grep
:
$ grep -o '[^|]*$' file
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
If you don't want leading spaces: 如果您不想要前导空格:
grep -o '[^|]*$' file | cut -d' ' -f2-
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
You can also use Bash parameter expansion: 您还可以使用Bash参数扩展:
$ while read -r line; do echo ${line##*|}; done <file
AMAZON-AES - Amazon.com, Inc., US
AMAZON-AES - Amazon.com, Inc., US
AMAZON-02 - Amazon.com, Inc., US
ECOM-AS ============================================, GB
ARUBA-ASN, IT
IT-STCOM, IT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.