Grep the last line and put on mail subject | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Grep the last line and put on mail subject

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-06-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 26 June 2014, 3:49 PM EDT
Posts: 204
Thanks: 77
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 03-06-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 1 August 2014, 6:49 PM EDT
Location: Saskatchewan
Posts: 19,068
Thanks: 759
Thanked 3,174 Times in 2,977 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 03-06-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 August 2014, 8:21 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,192 Times in 1,124 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 03-06-2013
Daniel Gate Daniel Gate is offline
Registered User
 
Join Date: May 2008
Last Activity: 26 June 2014, 3:49 PM EDT
Posts: 204
Thanks: 77
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 03-06-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 1 August 2014, 8:21 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,192 Times in 1,124 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 03-06-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 1 August 2014, 2:47 PM EDT
Location: Aachen, Germany
Posts: 3,950
Thanks: 63
Thanked 939 Times in 891 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 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 180 Times in 177 Posts
I would recommend to use tail, cut, and printf, as previously suggested.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 12:28 AM.