LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2013, 02:18 PM   #1
karthikbhuvanagiri
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Rep: Reputation: Disabled
Read a file, and send an email to the users inside the file


I am NEW TO UNIX, AND BEFORE i posted this here, I did search a whole lot, BUT couldnt seem TO find the answer. I have, this requirement whereI have to read a file generated BY a SQL output, and send out an email TO the email addresses the file has with a specific content.

This is the layout of my file

Karthik 03/18/2013 Karthik@xyz.com

I am using the following command to do the processing.

awk '{print " echo This is a sample email for " $1 " sent on " $3 " | mail -s \"Test EMAIL\" " $3 }' /file_location/filename.txt | ksh

$1,$2,$3 refer to the columns respectively, and this command is working fine and is giving me the ouptut ( when i remove the | ksh )

This is a sample email for Karthik sent on 03/18/2013 | mail -s "Highly Skewed Table in User Sandbox" karthik@xyz.com

And when executed I am getting a mail like this

This is a sample email for Karthik sent on 03/18/2013

My requirement is to add a signature to the email such as this

This is a sample email for Karthik sent on 03/18/2013

Thanks,
Karthik

I tried including echo \n in between , but it is breaking the command and throwing out the error , command not found , for eg:

awk '{print " echo This is a sample email for " $1 " sent on " $3 " echo \n " Thanks" echo \n "Karthik " " | mail -s \"Test EMAIL\" " $3 }' /file_location/filename.txt | ksh

This throws out the error saying that command thanks not found, can anyone tell me what am I missing and how this can be done.

Thanks,
Karthik
 
Old 03-18-2013, 03:16 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by karthikbhuvanagiri View Post
awk '{print " echo This is a sample email for " $1 " sent on " $3 " echo \n " Thanks" echo \n "Karthik " " | mail -s \"Test EMAIL\" " $3 }' /file_location/filename.txt | ksh
try /path/to/mail in script.
 
Old 03-18-2013, 03:30 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
awk '{print " echo This is a sample email for " $1 " sent on " $3 " echo \n " Thanks" echo \n "Karthik " " | mail -s \"Test EMAIL\" " $3 }' /file_location/filename.txt | ksh
I have to admit, this is a very creative way of doing this.
A few notes, though:
1) You have a mess in your double quotes. They should come in pairs. Also, quotes in shell do not nest (except for a few exceptions, such as command substitution).
2) Keep in mind that the \n sequences will be interpreted twice: once by awk and once by ksh. So, instead of making echo split the email into multiple lines, you actually make the awk split the command for ksh into multiple lines. You want those newlines to still be escaped when parsed by ksh. So, the solution would be to add one more backslash there, so you have \\n.
3) If you want to use escaped characters with echo, you need the -e switch.

That said, I still think that the " awk '{ print echo }' | ksh" construct is somewhat awkward. I think I would try something like this:

Code:
while read user msgtime address; do
    mail -s "Test EMAIL" "$address"<<EOF
This is a sample email for $user sent on $msgtime

  Thanks,
  Karthik 
EOF 
done
 
1 members found this post helpful.
Old 03-18-2013, 04:03 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
millgates, I think you mean
Code:
while read user msgtime address; do
    mail -s "Test EMAIL" "$address"<<EOF
This is a sample email for $user sent on $msgtime

  Thanks,
  Karthik 
EOF 
done < /file_location/filename.txt
Otherwise, this seems like the definitive solution.

--- rod.
 
1 members found this post helpful.
Old 03-18-2013, 04:06 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by theNbomr View Post
millgates, I think you mean
...
Ouch! I knew I forgot something Thanks for pointing that out
 
Old 03-18-2013, 05:53 PM   #6
karthikbhuvanagiri
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for replying, First of all, the while loop is working perfectly and it more than solves my purpose, thanks a ton for that.

But just out of curiosity, I wanted to understand where I was going wrong with the awk command, I tried \\n in the awk command, and though the command executes now, it still doesnt create a new line , it simply prints This is a sample email for karthik sent on 03/18/2013 n thanks n karthik .

Thanks,
Karthik

Last edited by karthikbhuvanagiri; 03-18-2013 at 06:06 PM.
 
Old 03-19-2013, 02:44 AM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by karthikbhuvanagiri View Post
But just out of curiosity, I wanted to understand where I was going wrong with the awk command, I tried \\n in the awk command, and though the command executes now, it still doesnt create a new line , it simply prints This is a sample email for karthik sent on 03/18/2013 n thanks n karthik .

Thanks,
Karthik
Did you use the -e switch with echo? Like

Code:
echo -e "Line1\nLine2"
Oh, and those quotes must be there (so, in your example, they have to be escaped to survive their way to the shell) otherwise the \n will be interpreted by the shell (This is a different thing than what I was describing earlier - before, the shell would get the actual newline, which means the command would come as multiple lines; in this case, the shell gets the string "\n", that is a backslash and an n character, and will try to interpret it - the meaning of backslash here is that the character after it loses its special meaning, regardles of whether it even had one, so an escaped n will translate to just n).
I hope this is not too confusing. Let's see some examples:

1)
Code:
awk 'BEGIN{ print "echo -e Line1\nLine2"}' | sh
In this case, awk will interpret the \n's and print:

Code:
echo -e Line1
Line2
the shell will get two separate lines, print the "Line1" part and then attempt to execute command "Line2", which it will most likely not find, so you get the "Line1: command not found" error.

2) Another example:

Code:
awk 'BEGIN{ print "echo -e Line1\\nLine2"}' | sh
Here, awk translates the "\\" to "\", so its output (and what the shell will receive) is:

Code:
echo -e Line1\nLine2
Now, the shell will translate the "\n" to just "n", so echo will get following argument: "Line1nLine2"

3) Another, correct example:

Code:
awk 'BEGIN{ print "echo -e \"Line1\\nLine2\""}' | sh
Again, in awk, \\ will become "\ and \" will become ". So, the shell will get

Code:
echo -e "Line1\nLine2"
Here, the \n is in double quotes, so the shell will not try to interpret it. echo will then get the intended argument:
Line1\nLine2

4) Another possibility is just adding another backslash:

Code:
awk 'BEGIN{ print "echo -e Line1\\\\nLine2"}' | sh
Here, two and two slashes will be translated by awk into single slashes, so the shell will get the following:

Code:
echo -e Line1\\nLine2"
The shell will then translate the "\\" into "\", so echo will get "Line1\nLine2" as its argument.

Last edited by millgates; 03-19-2013 at 02:47 AM.
 
1 members found this post helpful.
Old 03-19-2013, 06:01 PM   #8
karthikbhuvanagiri
LQ Newbie
 
Registered: Mar 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
That was the best explanation anyone could ever give, thanks man.

Regards,
Karthik
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LogZilla 3.x license file ( license.txt file) doesn’t send to my email account chamath Linux - Server 1 08-24-2011 06:39 AM
[SOLVED] How to read a file inside AWK? NabiVakili Programming 7 04-17-2011 03:44 PM
how to use expect - send to execute list of commands inside a file auma78 Linux - General 1 02-02-2011 12:00 PM
How to send email to a file for parsing archaegeo Linux - Newbie 1 10-31-2004 03:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration