Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-06-2013
Registered User
 
Join Date: May 2008
Posts: 180
Thanks: 75
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
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,591
Thanks: 501
Thanked 2,574 Times in 2,453 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
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,516
Thanks: 167
Thanked 821 Times in 789 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
Registered User
 
Join Date: May 2008
Posts: 180
Thanks: 75
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
Jedi Master
 
Join Date: Jan 2012
Location: Galactic Empire
Posts: 2,516
Thanks: 167
Thanked 821 Times in 789 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
Location: Aachen, Germany
Posts: 2,034
Thanks: 26
Thanked 463 Times in 448 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
Registered User
 
Join Date: Mar 2013
Posts: 858
Thanks: 18
Thanked 179 Times in 176 Posts
I would recommend to use tail, cut, and printf, as previously suggested.
Sponsored Links
Reply

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 08:22 AM.