• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

awk and mail x to outlook

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
atom_jelly
Asked:
atom_jelly
  • 6
  • 4
1 Solution
 
MikeOM_DBACommented:
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
 
atom_jellyAuthor Commented:
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
 
MikeOM_DBACommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
atom_jellyAuthor Commented:
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
 
MikeOM_DBACommented:
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
 
atom_jellyAuthor Commented:
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
 
atom_jellyAuthor Commented:
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
 
atom_jellyAuthor Commented:
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
 
MikeOM_DBACommented:
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
 
atom_jellyAuthor Commented:
Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now