簡體   English   中英

使用grep,awk或sed從列中獲取信息

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

我有以下表格:

^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

我想獲取每行的信息:num和address每列的開頭和結尾都使用我不需要的顏色代碼。 像在第1列中一樣,它以^ 5開頭,以^ 2結尾。也可以跳過帶有漫游器的行。

輸出我更喜歡:

0 77.18.76.12
5 84.18.8.144
12 22.185.55.9

我希望單獨顯示最后一行中顯示的玩家數量。

最好與sed,grep或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

如果記錄中有一個看起來像ip的字符串,請從記錄的開頭print它以及字符3和4。 如果玩家使用IP地址作為名稱,自然會失敗。

一個單獨的腳本來計算玩家:

$ 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