How to grep a string and add to subject line of a mail?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to grep a string and add to subject line of a mail?
# 1  
Old 08-03-2013
How to grep a string and add to subject line of a mail?

I am running a mailx command as follows in Linux:

Code:
mailx -s "Elapsed Time: " ora_dbas < $RUNDIR/sql_timings.out

I am trying to parse the file "sla_local_sql_timings.out" for the word Elapsed Time: and get the time from that file stored in a variable and display that variable in the subject line of my email. Is this possible at all?


Contents of sql_timing.out looks as below:
Code:
 
46 rows selected.
Elapsed: 00:10:00.10
SQL> 
SQL> spool off
 
46 rows selected.
Elapsed: 00:15:00.10
SQL> 
SQL> spool off

All i am trying to do is take the values "00:10:00.10" and "00:15:00.10" and add it to the subject line.

So the Subject line of my mail should be: Elapsed Time: 00:10:00.10 00:15:00.10


Can someone guide me on how to get this done please? thank you
Moderator's Comments:
Mod Comment Please use CODE tags to mark sample code and input and output samples. I guessed at where the CODE tags should go on your sample input and probably guessed incorrectly about where empty lines are supposed to appear.

Last edited by Don Cragun; 08-03-2013 at 12:51 AM.. Reason: Add CODE tags
# 2  
Old 08-03-2013
You could try something like:
Code:
#!/bin/ksh
InputFile="$RUNDIR/sql_timings.out"
Subject="Elapsed Time:$(awk '
/Elapsed:/ {
        c++
        printf(" %s", $2)
}
END {   printf("%s\n", c ? "" : " None");
}' "$InputFile")"
mailx -s "$Subject" ora_dbas < "$InputFile"

I tested it using the Korn shell, but it will also work with any other POSIX conforming shell (such as bash).

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 3  
Old 08-03-2013
Thank you for giving me your time on this. I have worked on Regex commands before and have some knowledge. I am now trying to educate myself on your code works so I can further refine it.

thanks again!
# 4  
Old 08-03-2013
This really is a pretty simple script, but if you aren't used to seeing all of the constiuent pieces combined together, it can be confusing. So, here is my script with line numbers added for discussion purposes. (This script will not work if the line numbers are included in the script.)
Code:
 1 #!/bin/ksh
 2 InputFile="$RUNDIR/sql_timings.out"
 3 Subject="Elapsed Time:$(awk '
 4 /Elapsed:/ {
 5        c++
 6        printf(" %s", $2)
 7 }
 8 END {  printf("%s\n", c ? "" : " None");
 9 }' "$InputFile")"
10 mailx -s "$Subject" ora_dbas < "$InputFile"

If you make this an executable script using the command:
Code:
chmod +x script_name

(where script_name is the name you chose to contain the original script), line 1 tells the system the name of the program to be used to interpret the remaining lines in the file. So, no matter what shell you are using when you invoke this script, /bin/ksh will be used to run this script.

Line 2 sets a variable containing the name of your input file. (It assumes that RUNDIR has been set as an exported shell variable before you call this script. I made that asuumption because your 1st message in this thread used this variable without showing how it was set.)

Lines 3-9 set the variable Subject to the string "Elapsed Time:" followed by the output of a command substitution. Command substitutions are of the form $(comand). In this case the command to be run is:
Code:
awk '
/Elapsed:/ {
       c++
       printf(" %s", $2)
}
END {  printf("%s\n", c ? "" : " None");
}' "$InputFile"

The lines in blue in this script look for lines that contain the string "Elapsed:". For every line that is found meeting that criteria, the variable c is incremented (all variables that haven't been set otherwise, have an initial value of 0 or an empty string depending on context) and then writes a space character followed by the contents of the 2nd field on that line (which is the elapsed time value you want to include in the subject line).

After every line in the file has been procesed, the commands in green associated with the special pattern END will be executed. In this case, it will print a string followed by a newline character. The string will be an empty string if c is not 0; otherwise, it will be the string " None".

And, the $InputFile on line 9 at the end of the awk script is the name of the file to be processed.

The trailing newline character written by this awk script will be removed as a side effect of command substitution.

Then line 10 sends mail to ora_dbas with the subject set to the string contained in the variable Subject and the body of the message being the contents of the file named by the variable InputFile.
# 5  
Old 08-03-2013
This makes sense to me now. Thank you very much. You have been very helpful.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

3. Shell Programming and Scripting

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 : DATE TIME TOTAL_GB USED_GB ... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

4. UNIX for Advanced & Expert Users

AIX - Sendmail - add hostname to subject of outgoing mail

Hello, I'm configuring sendmail on an AIX 7.1 server (bos.net.tcp.client 7.1.1.15). I've gotten sendmail to send mail through our Novell GroupWise server, so that mail from a user on the server appears to come from their GroupWise account, and replies to the email would go to their GroupWise... (0 Replies)
Discussion started by: eyebeam
0 Replies

5. Shell Programming and Scripting

Subject line missing while sending mail

Hi, I have below script PROJECT_NAME=UDL/UDL_Weekly sub= echo ${PROJECT_NAME}|cut -d "/" -f2 cat pr.sh|mail -s "`hostname`: $sub failed" sonu.pal@xyz.com While running the script I am receiving the subject line in mail as " podetlsapp01: failed' instead of " podetlsapp01: ... (1 Reply)
Discussion started by: sonu_pal
1 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

8. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

9. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. UNIX for Advanced & Expert Users

date need to be add in the subject of the mail.

Hi Am fetching a weekly report pf data..once i fetched the data i need a sent report by mail. In the subject of that mail i want to sent a message like.. SUBJECT :The report had been fetched from (01/12/08 to 07/12/08). I need to send a report like this every week with that particular... (2 Replies)
Discussion started by: bobprabhu
2 Replies
Login or Register to Ask a Question