0

I have file that has space separated columns from that i want to extract specific data .below is the format of the file :

12:00:01 AM     CPU      %usr     %nice      %sys   %iowait    %steal      %irq     %soft    %guest     %idle
12:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
12:02:01 AM     all     93.42      0.00      0.53      0.00      0.00      0.00      0.10      0.00      5.95
12:03:01 AM       1     88.62      0.00      1.71      0.00      0.00      0.00      0.71      0.00      8.96
12:01:01 AM       2     92.56      0.00      0.70      0.00      0.00      0.00      1.17      0.00      5.58
12:01:01 AM       3     86.90      0.00      1.57      0.00      0.00      0.00      0.55      0.00     10.99
01:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
01:02:01 AM     all     93.42      0.00      0.53      0.00      0.00      0.00      0.10      0.00      5.95
01:03:01 AM     all     88.62      0.00      1.71      0.00      0.00      0.00      0.71      0.00      8.96
01:01:01 AM       2     92.56      0.00      0.70      0.00      0.00      0.00      1.17      0.00      5.58
01:01:01 AM       3     86.90      0.00      1.57      0.00      0.00      0.00      0.55      0.00     10.99
12:01:01 PM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
12:02:01 PM       0     93.42      0.00      0.53      0.00      0.00      0.00      0.10      0.00      5.95
12:03:01 PM       1     88.62      0.00      1.71      0.00      0.00      0.00      0.71      0.00      8.96
12:01:01 PM       2     92.56      0.00      0.70      0.00      0.00      0.00      1.17      0.00      5.58
12:01:01 PM       3     86.90      0.00      1.57      0.00      0.00      0.00      0.55      0.00     10.99

Now from this file i want those rows that have time like 12:01:01 AM/PM i means for every hourly basis and have all in the CPU column
So after extraction i want below data but i am not able to get that.

12:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
01:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
12:01:01 PM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33

Please suggest me how we can get that data in UNIX

6
  • 1
    grep "01:01 .*all" yourFile.txt maybe? Commented Apr 7, 2016 at 11:54
  • yes i have tried that but i want that with awk or sed i have to create a new file Commented Apr 7, 2016 at 11:54
  • 1
    grep "01:01 .*all" yourFile.txt > newFile maybe? Commented Apr 7, 2016 at 11:55
  • i want that top column also how we can do that and that is not working for me Commented Apr 7, 2016 at 11:58
  • 1
    grep -E "CPU|01:01 .*all" yourFile > newFile maybe? Commented Apr 7, 2016 at 11:59

2 Answers 2

1

If you add the -E option to grep it allows you to look for "Extended Regular Expressions". One such expression is

"CPU|01:01"

which will allow you to find all lines containing the word "CPU" (such as your column heading line) and also any lines with "01:01" in them. It is called an "alternation" and uses the pipe symbol (|) to separate alternate sub-parts.

So, an answer would be"

grep -E "CPU|01:01 .*all" yourFile > newFile

Try running:

man grep

to get the manual (help) page.

Sign up to request clarification or add additional context in comments.

3 Comments

if have to do something like this grep -E "CPU|kbmemfree|kbswpfree|01:01 AM|PM.*all" will it work for me ??
No that will find, amongst other things, all lines that contain "PM" followed by "all" which, I guess, is not what you want. Please click edit under your question and describe accurately, in plain English (not code) what you want.
Mark actually in my log file i have multiple data and every data has same format but they have different column name .So what i want i want to extract the data of all those based on hourly basis
0

awk to the rescue!

if you need field specific matches awk is the right tool.

$ awk '$3=="all" && $1~/01:01$/' file

12:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
01:01:01 AM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33
12:01:01 PM     all     78.13      0.00      0.98      0.00      0.00      0.00      0.56      0.00     20.33

you can extract the header as well, with this

$ awk 'NR==1 || $3=="all" && $1~/01:01$/' file

2 Comments

Is the header first row? If not you can skip empty lines and print the first nonempty line as the header with this NF&&!f++, replace NR==1 with that and should work.
what is not working? header? perhaps add parenthesis NR==1 || ($3=="all" && $1~/01:01$/)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.