[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

awk and mail x to outlook

Posted on 2013-11-15
10
Medium Priority
?
278 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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)
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

649 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