Grep last two lines, calc & adding comments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep last two lines, calc & adding comments
# 1  
Old 06-09-2013
Grep last two lines, calc & adding comments

HTML Code:
.......
06/09/2013|12:00:00 PM|3|26112|40|44032|27419.7|6 1 0 93 |6|1|0|93
06/09/2013|12:30:00 PM|3|26112|40|44032|27491|11 4 0 85 |11|4|0|85

I have "sysperf.out" file containing the lines above.

What I like to have on the output is:

HTML Code:
Node: prod1db  ===> this is the hostname
Date: 06/09/2013 ===> which is the first column

Time: 12:00:00 PM (which is the 2nd column); Paging Space Util: 3% (3rd column); Memory Util: 62.27% (because 27419.7/44032*100); CPU Util: 6% (4th column from right)
Time: 12:30:00 PM (which is the 2nd column); Paging Space Util: 3% (3rd column); Memory Util: 62.43% (because 27419.7/27491*100); CPU Util: 11% (4th column from right)
So, the output will look like:

PHP Code:
Nodeprod1db
Date
06/09/2013

Time
12:00:00 PMPaging Space Util3%; Memory Util62.27%; CPU Util6%
Time12:30:00 PMPaging Space Util3%; Memory Util62.43%; CPU Util11
and then I want to put this on cron and send out e-mail.

Please advise.
# 2  
Old 06-09-2013
Show your effort so far.
# 3  
Old 06-10-2013
Code:
print `hostname`
print `date '+%m/%d/%Y'`
awk '{ L=$0 } END { $0=L ; print "Time:" $2,"Paging Space Util %:" $3,"Memory Util %:" $7/$6*100, "CPU Util %:" $8 }' sysperf.out

I have tried, but not working obviously. The calculation is not right, and because of the space and pipe ( | ), it is not getting the right values.

Please advise.

Last edited by Daniel Gate; 06-10-2013 at 01:07 AM..
# 4  
Old 06-10-2013
This awk program might work:
Code:
awk -F\| -v H="$( hostname )" '
        {
                if ( !P || ( P && P != $1 ) )
                        printf "Node: %s\nDate: %s\n\n", H, $1

                printf "Time: %s; ", $2
                printf "Paging Space Util: %d%%; ", $3
                printf "Memory Util: %.2f%%; ", ( $7 / $6 ) * 100
                printf "CPU Util: %d%%\n", $( NF - 3 )
        }
        {
                P = $1
        }
' sysperf.out

You have to specify the field separator as highlighted to distinguish the fields in your program.
# 5  
Old 06-10-2013
Thank you so much! Can I get only "today"'s output? This script runs for the entire rows...
# 6  
Old 06-10-2013
Quote:
Originally Posted by Daniel Gate
Thank you so much! Can I get only "today"'s output? This script runs for the entire rows...
Sure, use this modified code:
Code:
awk -F\| -v D="$( date +%m/%d/%Y )" -v H="$( hostname )" '
        BEGIN {
                printf "Node: %s\nDate: %s\n\n", H, D
        }
        $1 == D {
                printf "Time: %s; ", $2
                printf "Paging Space Util: %d%%; ", $3
                printf "Memory Util: %.2f%%; ", ( $7 / $6 ) * 100
                printf "CPU Util: %d%%\n", $( NF - 3 )
        }
' file

# 7  
Old 06-10-2013
wow!! you are the best!! That is exactly what I needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using Grep & find & while read line in a script

Hello people! I would like to create one script following this stage I have one directory with 100 files File001 File002 ... File100 (This is the format of content of the 100 files) 2012/03/10 12:56:50:221875936 1292800448912 12345 0x00 0x04 0 then I have one... (0 Replies)
Discussion started by: Abv_mx81
0 Replies

2. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

3. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

4. UNIX for Advanced & Expert Users

grep source code and exclude comments

I often find myself grepping source code for a variable name and many times the name would be present in comment lines that I 'd prefer not to see. Do you guys know any tricks to filter out comments? Example: snippet of the source code /*** * type comment 1 ***/ void ... (7 Replies)
Discussion started by: migurus
7 Replies

5. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

6. Shell Programming and Scripting

grep string & a few lines after

i need to grep a STRING_A & the next few lines after the STRING_A example file: STRING_A yada yada line 1 line 2 STRING_B yada yada line 1 line 2 line 3 STRING_A yada yada line 1 line 2 line 3 line 4 STRING_A yada yada line 1 line 2 line 3 line 4 (7 Replies)
Discussion started by: ashterix
7 Replies

7. UNIX for Advanced & Expert Users

Add Comments to the specifi lines i na file

I have a requirement like below.I need to Comment some lines in a file. File contains following information. { attribute1 attribute2 atrribute3 attribute4 attribute5 attribute6 attribute7 } I have a requirement like some times i need to comment lines 3 to before '}' and some... (1 Reply)
Discussion started by: ukatru
1 Replies

8. UNIX for Dummies Questions & Answers

How to put the comments to 50 lines, using vi editor?

Hi All, Please let me know how I can put a comment (e.g // or #) to more than 50 lines using vi editor in a .cpp/.sh file. Thanks in advance. (3 Replies)
Discussion started by: artikulkarni
3 Replies

9. Shell Programming and Scripting

grep string & next n lines

need help on this. let say i hv 1 file contains as below: STRING Description bla bla bla Description yada yada yada Data bla bla Data yada yada how do i want to display n lines after the string? thanks in advance! (8 Replies)
Discussion started by: ashterix
8 Replies
Login or Register to Ask a Question