簡體   English   中英

txt文件中一列的GREP值

[英]GREP values from a column in txt file

我有一個以這種方式包含1200個條目的txt文件(通過iPerf輸出)

1  [  4]  0.0- 1.0 sec  10.6 MBytes  89.1 Mbits/sec
2  [  4]  1.0- 2.0 sec  13.5 MBytes   113 Mbits/sec
3  [  4]  2.0- 3.0 sec  9.50 MBytes  79.7 Mbits/sec
4  [  4]  3.0- 4.0 sec  9.00 MBytes  75.5 Mbits/sec

如何使用grep獲得以Mbits / sec表示的第二個值?

輸出示例:

89.1
113
79.7
75.5
awk '{print $9}' your-file.txt 

會為你做。 例如:

$ cat ~/test.txt
1  [  4]  0.0- 1.0 sec  10.6 MBytes  89.1 Mbits/sec
2  [  4]  1.0- 2.0 sec  13.5 MBytes   113 Mbits/sec
3  [  4]  2.0- 3.0 sec  9.50 MBytes  79.7 Mbits/sec
4  [  4]  3.0- 4.0 sec  9.00 MBytes  75.5 Mbits/sec

$ awk '{print $9}' ~/test.txt
89.1
113
79.7
75.5

解決此問題的另一種方法是:

 awk -F 'MBytes' '{print $2}' test.txt | awk -F 'Mbits' '{print $1}' | tr -d " "

在上面的方法中,我們是:

  • 將每一行拆分為兆字節。
  • 這給了我們2個部分:$ 1是MBytes之前的所有內容。 $ 2是MBytes之后的所有內容
  • 我們選擇兆字節后的所有內容,然后按兆位進一步划分
  • 這又給了我們兩個部分,我們選擇了Mbits之前的所有內容
  • 如果數字前后有空格,則使用tr刪除空格

所以我們得到

$ cat test.txt
1  [  4]  0.0- 1.0 sec  10.6 MBytes  89.1 Mbits/sec
2  [  4]  1.0- 2.0 sec  13.5 MBytes   113 Mbits/sec
3  [  4]  2.0- 3.0 sec  9.50   MBytes  79.7 Mbits/sec
4  [  4]  3.0- 4.0 sec  9.00 MBytes    75.5 Mbits/sec

awk -F 'MBytes' '{print $2}' test.txt | awk -F 'Mbits' '{print $1}' | tr -d " "

Result:
89.1
113
79.7
75.5

如果您的數據是固定長度格式,則可以隨時使用cut

cut -c38-41 data

如果您知道值的寬度為4個字符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM