Shell Script to read hive table and send email


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell Script to read hive table and send email
# 1  
Old 12-19-2019
Shell Script to read hive table and send email

HI Team,
I am working on reading hive table and send email in email body using shell script, can you please help on fixing the errors: I have 6 columns in my hive table and trying to send the email in the mail body.
below script:

Code:
#!/bin/bash
hive -e 'SELECT count(*) from db.table' > output.txt
sed -i 's/\t/,/g' $tmp/output.txt

nawk BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML>""<TABLE border="1"><TH>TB1</TH><TH>TB2</TH><TH>TB3</TH><TH>TB4</TH><TH>TB5</TH><TH>TB6</TH>" }
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }
file-to-convert.csv > file.html
echo "output.txt" | mailx -s "output" -a output.txt email.com

--- Post updated at 04:21 PM ---

getting below error:

Code:
Java HotSpot(TM) 64-Bit Server VM warning: Using the ParNew young collector with the Serial old collector is deprecated and will likely be removed in a future release
OK
Time taken: 4.722 seconds, Fetched: 5 row(s)
Waiting for Tez session and AM to be ready...
sed: can't read $tmp/output.csv: No such file or directory
./countemail.sh: line 18: nawk: command not found
./countemail.sh: line 19: output1.csv: command not found


Last edited by Neo; 12-19-2019 at 12:13 PM.. Reason: Code Tags Please See YT Video on this: https://youtu.be/4BuPvWJV__k
# 2  
Old 12-19-2019
I am missing something here...
./countemail.sh: : Is that the script you gave us ?
Since when a csv file is a script?
Code:
 file-to-convert.csv > file.html

# 3  
Old 12-19-2019
yes countemail.sh is the script and below is the code:

--- Post updated at 07:51 PM ---

complete script
Code:
#!/bin/bash
hive -e 'select * FROM db.table' > output1.csv
sed -i 's/\t/,/g' output1.csv
nawk BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML>""<TABLE border="1"><TH>project_name</TH><TH>schema_name</TH><TH>table_name</TH><TH>partition_col_name</TH>"}
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }''
output1.csv > file.html
echo "file.html" | mailx -s "output" -a file.html email


Last edited by Chubler_XL; 12-19-2019 at 03:55 PM.. Reason: Code Tags Please See YT Video on this: https://youtu.be/4BuPvWJV__k
# 4  
Old 12-19-2019
Firstly the posted script seems to be missing single quote at the start of the awk code, and two single quotes at the end.

Also your system is reporting that nawk is not installed nawk is usually required on Solaris systems, you don't report your OS but my guess is a non-Solaris system. Try using awk or gawk instead.

Try:
Code:
#!/bin/bash
hive -e 'select * FROM db.table' > output1.csv
sed -i 's/\t/,/g' output1.csv
awk 'BEGIN{
FS=","
print  "MIME-Version: 1.0"
print  "Content-Type: text/html"
print  "Content-Disposition: inline"
print  "<HTML>""<TABLE border="1"><TH>project_name</TH><TH>schema_name</TH><TH>table_name</TH><TH>partition_col_name</TH>"}
 {
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
 }
END{
print "</TABLE></BODY></HTML>"
 }' output1.csv > file.html
echo "file.html" | mailx -s "output" -a file.html email

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 12-20-2019
Thank you very much, this code worked. Appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to output the prstat into table and send through email?

Hi how to send below prstat script output into table format and send through email? prstat|awk '{ if ($9 >= 5) print $0 }' (17 Replies)
Discussion started by: tharmendran
17 Replies

2. Shell Programming and Scripting

Shell scripting unable to send the sql query data in table in body of email

I have written a shell script that calls below sql file. It is not sending the query data in table in the body of email. spool table_update.html; SELECT * FROM PROCESS_LOG_STATS where process = 'ActivateSubscription'; spool off; exit; Please use code tags next time for your code and data.... (9 Replies)
Discussion started by: Sharanakumar
9 Replies

3. Shell Programming and Scripting

how to send html email from shell script

This is my below script which is working fine as it send only plain text email. So My question is- How can I modify my below script to send this as an HTML formatted email? I want to show the text color of this expression `(echo "100*$TEST2/$TEST1" | bc -l)` as `RED` in an email and that can be... (3 Replies)
Discussion started by: raihan26
3 Replies

4. Shell Programming and Scripting

Help with shell script to send email once

Hi Guys, I have this script which will monitor oracle db process if up or down.And I want it to send email if it's down and the time it's back to online. However my script just keep on sending "Email Up" if the db is up or "Email Down" if the db is down.Is there any way to trap it so that it... (5 Replies)
Discussion started by: d3xt3r
5 Replies

5. Shell Programming and Scripting

Shell script to send an email from the txt file

Hi Friends, Could you guys help me out of this problem... I need to send an email to all the users and the email has to be picked from the text file. text file contains the no. of records like: giridhar 224285 847333 giridhar276@gmail.com ramana 84849 33884 venkata.ramana@gmail.com... (6 Replies)
Discussion started by: giridhar276
6 Replies

6. Shell Programming and Scripting

How to read specific line of text from a Script and send email notification

Hi ! I am a newbie and never officially wrote a shell script before. The requirement for this script is : 1) Read a file called 'bpm.log' and identify if it has a specific text such as 'this is the text'. Its a static value and that is the only text we need to read. 2) If that... (2 Replies)
Discussion started by: atechcorp
2 Replies

7. Shell Programming and Scripting

Script to send email after comparing the folder permissions to a certain permission & send email

Hello , I am trying to write a unix shell script to compare folder permission to say drwxr-x-wx and then send an email to my id in case the folders don't have the drwxr-x-wx permissions set for them . I have been trying to come up with a script for few days now , pls help me:( (2 Replies)
Discussion started by: nairshar
2 Replies

8. Shell Programming and Scripting

How to send email through shell script

Hi All, I am new to the unix , i have to deliver one script very urgently I have to write a shell script where i have i want to send email to specific email id in this script i want FROM to be parameterized and stored in a variable TO to be parameterized and stored in a variable... (3 Replies)
Discussion started by: nileshbhawsar
3 Replies

9. Shell Programming and Scripting

make un shell script to send email

Hi, Someone Knows how to obtein a chain from a unix file, but not all the line, for exemple, for this file ,obtein only 902111111 and to keep the value, this value will be used to make a mailx. NF=ALL, SUBJ= FROM: SN=CD, SE=TOPCALL, NA=, N=902111111, N=TCLFI TO: SE=FAX, NA=, C1=... (0 Replies)
Discussion started by: peybol
0 Replies

10. Shell Programming and Scripting

How to write a shell script to send an email to an id

Hi all, I want to know how to send an email using shell script ..... I tried a command call mail but it did not work.. Please any one reply.. With example if you know thanks.. Madhu (4 Replies)
Discussion started by: madhumathikv
4 Replies
Login or Register to Ask a Question