Solved

awk and mail x to outlook

Posted on 2013-11-15
10
246 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
  • 6
  • 4
10 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:atom_jelly
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:atom_jelly
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now