简体   繁体   English

使用grep,awk或sed从列中获取信息

[英]info grabbing from a column with grep, awk or sed

I have the following tabel: 我有以下表格:

^2num^5|^2score^5|^2ping^5 | ^2status ^5| ^2name players      ^5|        ^2address
---- ------- ------ -------- -------------------- -----------------------^7
^5 0 ^2|^3  41 ^2|^3 100 ^2|^5 Player ^2|^7Just a Cr4zy name!   ^2|^77.18.76.12:58641   ^3[^5FR^3]^7
^5 2 ^2|^3   3 ^2|^3  57 ^2|^3  Bot   ^2|^7^8bot1                   ^2|^7bot
^5 3 ^2|^3   7 ^2|^3  43 ^2|^3  Bot   ^2|^7^8bot2                   ^2|^7bot
^5 4 ^2|^3  18 ^2|^3  16 ^2|^3  Bot   ^2|^7^8bot3                   ^2|^7bot
^5 5 ^2|^3   2 ^2|^3 103 ^2|^5 Player ^2|^7Just a ^5Cr4zy n4me2!   ^2|^784.18.8.144:27960   ^3[^5IL^3]^7
^5 6 ^2|^3  18 ^2|^3 102 ^2|^3  Bot   ^2|^7^8bot4                   ^2|^7bot
^5 7 ^2|^3  29 ^2|^3 102 ^2|^3  Bot   ^2|^7^8bot5                   ^2|^7bot
^5 8 ^2|^3  39 ^2|^3  54 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^5 9 ^2|^3  24 ^2|^3  77 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^510 ^2|^3  10 ^2|^3 103 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^511 ^2|^3  42 ^2|^3  95 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^512 ^2|^3   2 ^2|^3 103 ^2|^5 Player ^2|^7Ju5t a ^5Cr4zy ^7name3!      ^2|^722.185.55.9:13565   ^3[^5IL^3]^7
^513 ^2|^3  24 ^2|^3  96 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^514 ^2|^3   0 ^2|^3  51 ^2|^3  Bot   ^2|^7^8bot                    ^2|^7bot
^2-------------------------------------------------------------------------^7
^5Bots : ^311 ^5, Players : ^33 ^5, All : ^314

I would like to grab the info per line: num and address Every column starts and ends with a color code i do not need. 我想获取每行的信息:num和address每列的开头和结尾都使用我不需要的颜色代码。 Like in column 1, it starts with ^5 and ends with ^2 The lines with a bot can also be skipped. 像在第1列中一样,它以^ 5开头,以^ 2结尾。也可以跳过带有漫游器的行。

output i prefer: 输出我更喜欢:

0 77.18.76.12
5 84.18.8.144
12 22.185.55.9

Seperate i would like to have the number of players as displayed in the last line. 我希望单独显示最后一行中显示的玩家数量。

preferable with sed, grep or awk 最好与sed,grep或awk一起使用

Thank you very much in advance. 提前非常感谢您。

In awk: 在awk中:

$ awk 'match($0,/([0-9]{1,3}\.){3}[0-9]{1,3}/) {
           print substr($0,3,2),substr($0,RSTART,RLENGTH) }
 0 77.18.76.12
 5 784.18.8.144
12 722.185.55.9

If there is an ip looking string in the record, print it and characters 3 and 4 from the beginning of the record. 如果记录中有一个看起来像ip的字符串,请从记录的开头print它以及字符3和4。 Naturally will fail if player uses an ip address as name. 如果玩家使用IP地址作为名称,自然会失败。

A separate script to count the players: 一个单独的脚本来计算玩家:

$ awk 'match($0,/([0-9]{1,3}\.){3}[0-9]{1,3}/) {
           i++ }           # player counter
       END{ print "Players : " i+0 }' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM