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 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 “Timestamp”, but it fetched the complete data irrespective of CPU Usage/Memery Usage.
How can i do this so that i have the the data as required.
Thanks in advance.
Advertisement
Answer
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
. grep
is the great tool for finding/filtering the content you want to process further down the line.