?
Solved

awk and mail x to outlook

Posted on 2013-11-15
10
Medium Priority
?
272 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

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!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

777 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