简体   繁体   English

文本文件中日期的grep用法

[英]grep usage for date in a text file

I have some data in a text file like below: 我在文本文件中有一些数据,如下所示:

Sample 0: CPU Usage: 2.56% Memory Usage: 5608.00 KiB Timestamp: 2015/08/06 16:46:35:955 
Sample 1: CPU Usage: 1.21% Memory Usage: 5576.00 KiB Timestamp: 2015/08/06 16:46:37:143 
Sample 2: CPU Usage: 2.46% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:38:323 
Sample 3: CPU Usage: 2.49% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:39:502 
Sample 4: CPU Usage: 2.51% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:40:674 
Sample 5: CPU Usage: 2.56% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:41:832 
Sample 6: CPU Usage: 2.21% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:43:009 
Sample 7: CPU Usage: 1.92% Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:44:202 
Sample 8: CPU Usage: 2.83% Memory Usage: 5572.00 KiB Timestamp: 2015/08/06 16:46:45:408 
Sample 9: CPU Usage: 1.95% Memory Usage: 5572.00 KiB Timestamp: 2015/08/06 16:46:46:575 
Sample 10: CPU Usage: 1.74% Memory Usage: 5572.00 KiB Timestamp: 2015/08/06 16:46:47:734

Now i want to fetch CPU Usage/Timestamp pair at a time like below: 现在我想一次获取CPU使用率/时间戳对,如下所示:

CPU Usage: 1.74% Timestamp: 2015/08/06 16:46:47:734
CPU Usage: 1.95% Timestamp: 2015/08/06 16:46:46:575

And, Memory Usage/Timestamp pair at a time like below: 而且,每次使用的“内存使用/时间戳”对如下所示:

Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:47:734
Memory Usage: 5560.00 KiB Timestamp: 2015/08/06 16:46:46:575

I tried grep -i "CPU Usage" testFile.txt | 我试过grep -i“ CPU使用率” testFile.txt | grep -i "Timestamp", but it fetched the complete data irrespective of CPU Usage/Memery Usage. grep -i“时间戳记”,但是无论CPU使用率/内存使用率如何,它都能获取完整的数据。

How can i do this so that i have the the data as required. 我该怎么做,以便我有所需的数据。

Thanks in advance. 提前致谢。

If all your data fields are actually seperated by spaces, you could do something like this: 如果所有数据字段实际上都由空格分隔,则可以执行以下操作:

  cut -d ' ' -f 3,4,5,10,11,12 < testFile.txt > cpu.txt 
  cut -d ' ' -f 6-12 < testFile.txt > mem.txt 

If your data is not so simply structured you might be better off using awk . 如果您的数据结构不是那么简单,那么使用awk可能会更好。 grep is the great tool for finding/filtering the content you want to process further down the line. grep是查找/过滤要进一步处理的内容的好工具。

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

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