Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Grep the last line and put on mail subject

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-06-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 4 August 2016, 8:55 AM EDT
Posts: 233
Thanks: 91
Thanked 0 Times in 0 Posts
Grep the last line and put on mail subject

I have mail:
cat /home/oracle/scripts/dbsizedaily.txt | mail -s "$TODAY: PROD DB Size" $RECIPIENTS

I like to get and put USED_GB and %USED of the very last row from /home/oracle/scripts/dbsizedaily.txt .

/home/oracle/scripts/dbsizedaily.txt has :
HTML Code:
DATE      TIME       TOTAL_GB    USED_GB      %USED
......                     
03/05/2013 21:13 PM 3151.24316 2329.82947 73.9336619                        
03/05/2013 21:17 PM 3151.24316 2330.17322 73.9445703
so that e-mail subject that contain
"DATE: 03/06/13 - PROD DB Size: 2,330.17 GB - %USED:73.94"

Please advise.
Sponsored Links
    #2  
Old Unix and Linux 03-06-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 August 2016, 5:27 PM EDT
Location: Saskatchewan
Posts: 21,430
Thanks: 967
Thanked 3,898 Times in 3,627 Posts
grep matches regular expressions, it doesn't understand concepts like the 'last line'.

awk does however. I'd try this:


Code:
awk '{ L=$0 } END { $0=L ; print "DATE: "$1,"PROD DB SIZE: "$5,"%USED:"$6 }' OFS=" - " /home/oracle/scripts/dbsizedaily.txt |
        mail -s "$TODAY: PROD DB Size" $RECIPIENTS

Sponsored Links
    #3  
Old Unix and Linux 03-06-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 August 2016, 7:51 PM EDT
Location: Galactic Empire
Posts: 3,520
Thanks: 248
Thanked 1,276 Times in 1,198 Posts
OR use tail and cut to get those field values:

Code:
USED_DB=$(tail -1 /home/oracle/scripts/dbsizedaily.txt | cut -d' ' -f5)
USED_PC=$(tail -1 /home/oracle/scripts/dbsizedaily.txt | cut -d' ' -f6)

cat /home/oracle/scripts/dbsizedaily.txt | mail -s "$TODAY: PROD DB Size: $USED_DB - %USED: $USED_PC"

    #4  
Old Unix and Linux 03-06-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 4 August 2016, 8:55 AM EDT
Posts: 233
Thanks: 91
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by bipinajith View Post
OR use tail and cut to get those field values:

Code:
USED_DB=$(tail -1 /home/oracle/scripts/dbsizedaily.txt | cut -d' ' -f5)
USED_PC=$(tail -1 /home/oracle/scripts/dbsizedaily.txt | cut -d' ' -f6)

cat /home/oracle/scripts/dbsizedaily.txt | mail -s "$TODAY: PROD DB Size: $USED_DB - %USED: $USED_PC"

Is there any way to format the number into two decimals with a comma so that they show as PROD DB Size: 2,330.17 GB - %USED:73.94 rather than PROD DB Size: 2330.17322 - %USED: 73.9445703 ?
Sponsored Links
    #5  
Old Unix and Linux 03-06-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 28 August 2016, 7:51 PM EDT
Location: Galactic Empire
Posts: 3,520
Thanks: 248
Thanked 1,276 Times in 1,198 Posts
You can use printf to format the number to 2 decimal places and include thousand separators:

Code:
USED_DB=$( printf "%'.2f" $USED_DB )
USED_PC=$( printf "%'.2f" $USED_PC )

---------- Post updated at 17:16 ---------- Previous update was at 17:13 ----------

From printf manual:

Code:
A block size specification preceded by `'' causes output sizes to be
displayed with thousands separators.  The `LC_NUMERIC' locale specifies
the thousands separator and grouping.  For example, in an American
English locale, `--block-size="'1kB"' would cause a size of 1234000
bytes to be displayed as `1,234'.  In the default C locale, there is no
thousands separator so a leading `'' has no effect.

Sponsored Links
    #6  
Old Unix and Linux 03-06-2013
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 28 August 2016, 2:27 PM EDT
Location: Aachen, Germany
Posts: 9,255
Thanks: 181
Thanked 2,701 Times in 2,528 Posts
A little experiment:
Code:
awk 'END {print "\"DATE: "$1 "- PROD DB SIZE: ", $5+0, "- %USED:", $6-0, "\" ", REC |"mail -s"}' OFMT="%.2f" REC=$RECIPIENTS /home/oracle/scripts/dbsizedaily.txt

Sponsored Links
    #7  
Old Unix and Linux 03-06-2013
hanson44 hanson44 is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 12 May 2013, 11:33 PM EDT
Posts: 858
Thanks: 18
Thanked 182 Times in 178 Posts
I would recommend to use tail, cut, and printf, as previously suggested.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Subject line missing while sending mail sonu_pal Shell Programming and Scripting 1 06-13-2012 08:32 AM
how to put subject and body to mail from two files sri1977 Shell Programming and Scripting 1 10-10-2011 09:40 AM
Perl - how to put to the next line if I have same pattern in one line askari Programming 2 07-30-2011 01:30 PM
Remove line based on string and put new line with parameter victor369 Shell Programming and Scripting 4 05-26-2011 09:49 AM
ls | grep (i dont know what to put here) kevincobain2000 Shell Programming and Scripting 4 05-02-2011 11:56 PM



All times are GMT -4. The time now is 11:49 PM.