Solved

awk and mail x to outlook

Posted on 2013-11-15
10
266 Views
Last Modified: 2013-12-18
I currently run a script that executes commands and sends the output to a file , my problem is that the format in my temp file does not have the last line return to the next line.



the file contents before mailing appears like this:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z
 Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.493s)
Planning: Running solver ... Done (5.096s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (3.865s)
Planning: Planning completed in 16.55 seconds
No updates available for this image.





my script:
cat $tmpfile | awk '{if(NR==3){print "\n"}else{print $0,"\r"}}' |  mailx  -s "Solaris 11 Repository Status"


When I receive it in my outlook it looks like this.



PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z


 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.493s)
Planning: Running solver ... Done (5.096s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (3.865s)
Planning: Planning completed in 16.55 seconds No updates available for this image.

notice that the No updates does not go to the next line.
0
Comment
Question by:atom_jelly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39652522
The issue may be how mailx interprets the message, it takes the period (.) as end of a paragraph.

You may either need to supply a period at the end of every line or  "\n" instead of "\r".
0
 

Author Comment

by:atom_jelly
ID: 39653140
Can you give me a visual example? I have tried \n but it seems to insert a newline in between the lines and putting a single space between each line. I have my awk if statement the way it is because I need these records together.

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z


Thanks.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39657373
Without having an EXACT copy (contents) http://www.experts-exchange.com/Programming/Languages/Q_28295522.html#of your tempfile, it is difficult to determine the reason for your issue.

If you just want to add a line before the third line, try this:
awk '{if(NR==3){print "\n"};print $0;}' $tmpfile | mailx  -s "Solaris 11 Repository Status" me@mydomain.com

Open in new window

0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:atom_jelly
ID: 39662934
I apologize for not including an exact copy of my file.

Just to add here are the contents of what my script exactly produces:

my $tmpfile:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z
 Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.477s)
Planning: Running solver ... Done (5.077s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (4.092s)
Planning: Planning completed in 16.75 seconds
No updates available for this image.


I want it to be sent via mailx in this format:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z

Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.477s)
Planning: Running solver ... Done (5.077s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (4.092s)
Planning: Planning completed in 16.75 seconds
No updates available for this image.



Now when I run the script my self it sends it to me exactly like this:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z


 Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.477s)
Planning: Running solver ... Done (5.077s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (4.092s)
Planning: Planning completed in 16.75 seconds No updates available for this image.

When my cron job runs it at night it only sends it from "Super-User" and looks like this:

 Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done


Planning: Running solver ... Done (5.041s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (3.901s)
Planning: Planning completed in 16.54 seconds
No updates available for this image.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39663500
If you need to receive the file EXACTLY as is, without mailx or the recipient mail service (re)-formating it  --  then just attach it!

$ uuencode $tmpfile $tmpfile | mailx  -s "Solaris 11 Repository Status" me@mymail.com

Open in new window

0
 

Author Comment

by:atom_jelly
ID: 39666185
Thank you for the one-liner and making it an attachment but I am tasked with having it e-mailed with the body of my messages with the first two lines together and the rest of the message separated with newline space.

Like this:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z


 Startup: Refreshing catalog 'solaris' ... Done
 Startup: Checking that pkg(5) is up to date ... Done
Planning: Solver setup ... Done (5.493s)
Planning: Running solver ... Done (5.096s)
Planning: Consolidating action changes ... Done (0.004s)
Planning: Evaluating mediators ... Done (3.865s)
Planning: Planning completed in 16.55 seconds
No updates available for this image.
0
 

Author Comment

by:atom_jelly
ID: 39669177
How could I extract the first two lines and a third newline and get the last line and mailx it to me?

Like this:

PUBLISHER PACKAGES STATUS           UPDATED
solaris   4401     online           2013-10-15T18:17:38.417509Z

No updates available for this image.

This is my current script:

#!/bin/bash
#This script just checks for the latest updates at pkg.oracle.com
now=`date +"%m_%d_%Y"`
infile=/tmp/repostatus
outfile=/tmp/repostatus.$now
#CHECKING FOR UPDATES AND PUTTING THEM IN aipool/s11Repo
pkgrecv '*'
#REFRESHING THE CATALOGUE OF ANY UPDATES
pkgrepo refresh -s file:///aipool/s11Repo
#CHECKING FOR THE LATEST DATE THE REPOSITORY WAS UPDATED.
pkgrepo info -s http://pkg.oracle.com/solaris/release >$infile
#OUTPUT ANY UPDATES WITHOUT INSTALLING THEM.
pkg update -nv --accept >>$infile
awk '{if (NR==3)printf("\n\n");print $0," "}' $infile > $outfile
rm $infile
#EMAIL THE RESULTS
#THE AWK IS SAYING AFTER THE FIRST TWO RECORDS OR LINES PRINT OUTPUT
cat $outfile |  mailx -s "Solaris 11 Repository Status"  andreas.magana.ctr@us.af.mil
#cat $tmpfile | awk '{if(NR==3){print "\n"}else{print $0,"\r"}}' |  mailx  -s "Solaris 11 Repository Status"  me@domain
0
 

Author Comment

by:atom_jelly
ID: 39690157
I at least put a if then else statement at the end and just manipulate what to send from one file that status file.

if grep -q 'No updates' "$status"
then
    cat "$outfile" | head -3 > "$status" ; cat "$outfile" | tail -1 >> "$status"
    cat "$status"  |  mailx -s "Solaris 11 Repository Status"  me@domain
else
    cat "$outfile" | mailx -s "Solaris 11 Repository Status"  me@domain
fi
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 39693393
Or...
# ....
if grep -q 'No updates' "$status"
then
    awk 'NR<3{print }{ll=$0}END{print "\n\n\n"ll"\n."}' $outfile|tee -a $status |\
    mailx  -s "Solaris 11 Repository Status" me@mydomain.com
else
    cat "$outfile" | mailx -s "Solaris 11 Repository Status"  me@mydomain.com
fi

Open in new window

0
 

Author Closing Comment

by:atom_jelly
ID: 39727958
Thank you.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question