Get output of fields starting from 2nd line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get output of fields starting from 2nd line
# 1  
Old 02-18-2013
Get output of fields starting from 2nd line

Hi All,

I am using the following command in Linux:

Code:
 
sar -r 30 3
Linux 2.6.18-194.3.1.7.3.el5xen  02/07/2013

02:55:47 PM kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
02:56:17 PM 128646024  22348920     14.80    230232  15575860  75497464         0      0.00         0
02:56:47 PM 128646188  22348756     14.80    230244  15576116  75497464         0      0.00         0
02:57:17 PM 128646676  22348268     14.80    230248  15576160  75497464         0      0.00         0
Average:    128646296  22348648     14.80    230241  15576045  75497464         0      0.00         0
-bash-3.2$


I have tried using the following command to get the desired output but somehow not getting it. %memused field is blank

Code:
 
-bash-3.2$ sar -r 1 3 | grep -v Average | grep -v Linux | cut -d' ' -f1,2,6

12:57:57 PM %memused
12:57:58 PM
12:57:59 PM
12:58:00 PM
-bash-3.2$

From the above output, I need to get the following output onwards.

Output should look as below( Need only the values and not the columns to be displayed):

Code:
 
02:55:47 PM    %memused 
02:56:17 PM      14.80    
02:56:47 PM      14.80    
02:57:17 PM      14.80

Thanks for your time!

Regards,
a1_win

Moderator's Comments:
Mod Comment edit by bakunin: corrected your CODE-tags: the opening one is always without the slash, only the closing one has it: [tag]...[/tag]

Last edited by bakunin; 02-18-2013 at 05:22 PM..
# 2  
Old 02-18-2013
Code:
sar -r 1 3 | awk '/^[0-9]/ {print $1,$2,$5}'
02:55:47 PM %memused
02:56:17 PM 14.80
02:56:47 PM 14.80
02:57:17 PM 14.80

PS remove the / in your start code tag [/code]
# 3  
Old 02-18-2013
bash has a nice feature. You declare a simple array variable 'typedef -a myarray' and then 'while read -a myarray' and the fields are in the array for each line. Awk does that sort of thing natively.

BTW, free memory is a worthless spec in a VM system. That stat is left over from the old days of memory resident everything. delta page faults is more interesting, and load (cpu avg queue depth).
# 4  
Old 02-18-2013
Quote:
Originally Posted by a1_win
Code:
sar -r 30 3
Linux 2.6.18-194.3.1.7.3.el5xen  02/07/2013

02:55:47 PM kbmemfree kbmemused  %memused kbbuffers  kbcached kbswpfree kbswpused  %swpused  kbswpcad
02:56:17 PM 128646024  22348920     14.80    230232  15575860  75497464         0      0.00         0
02:56:47 PM 128646188  22348756     14.80    230244  15576116  75497464         0      0.00         0
02:57:17 PM 128646676  22348268     14.80    230248  15576160  75497464         0      0.00         0
Average:    128646296  22348648     14.80    230241  15576045  75497464         0      0.00         0

Instead of trying to filter away everything you do not want it is sometimes easier to specify what you do want: ever line where "<time> PM " is followed by a digit, not a letter, right?

Code:
grep '^[0-2][0-9]:[0-6][0-9]:[0-6][0-9] PM [0-9]'

Should filter all the lines you are interested in. As you use "cut" afterwards to dissect the lines you can combine this regexp and the "cut"ting into an awk- or sed-script, like Jotne already did - alas, his filter regexp is wrong:

Code:
sar -r 1 3 | awk '/^[0-2][0-9]:[0-6][0-9]:[0-6][0-9] PM [0-9]/ {print $1,$2,$5}'

I have to agree with DGPickett: you probably are extracting not particular meaningful numbers and might want to give your methodology a second thought.

I hope this helps.

bakunin
# 5  
Old 02-19-2013
Thanks this has helped!

Regards,
a1_win
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

3. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

4. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

5. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

6. Shell Programming and Scripting

How to print line starting with certain string together with its following line?

Dear all, How can I print line starting with certain string together with its following line. Example is as follows: Input file: @M01596:22:000000000-A7YH7:1:1101:16615:1070 2:N:0:1... (2 Replies)
Discussion started by: huiyee1
2 Replies

7. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

8. UNIX for Dummies Questions & Answers

Write 2nd and 3rd fields to a 4th file name?

I have a flatfile A.txt date|products|notes|location 121117|a108|this is a test|florida 121118|b111|just test it|tampa How do i write an awk to create a file name as location.txt and have products:notes awk -F'|' '{ print $2 ":" $3 }' A.txt > $4.txt I am sure it cannot write to... (5 Replies)
Discussion started by: sabercats
5 Replies

9. Shell Programming and Scripting

Compare multiple fields in file1 to file2 and print line and next line

Hello, I have two files that I need to compare and print out the line from file2 that has the first 6 fields matching the first 6 fields in file1. Complicating this are the following restrictions 1. file1 is only a few thousand lines at most and file2 is greater than 2 million 2. I need to... (7 Replies)
Discussion started by: gillesc_mac
7 Replies

10. Shell Programming and Scripting

how to delete text from line starting pattern1 up to line before pattern2?

My data is xml'ish (here is an excerpt) :- <bag name="mybag1" version="1.0"/> <contents id="coins"/> <bag name="mybag2" version="1.1"/> <contents id="clothes"/> <contents id="shoes"/> <bag name="mybag3" version="1.6"/> I want to delete line containing mybag2 and its subsequent... (5 Replies)
Discussion started by: repudi8or
5 Replies
Login or Register to Ask a Question