Thursday, 4 August 2016

How To Fetch Specific Field In Multi Character Delimiter File Using AWK

In earlier articles we have seen awk command to fetch a specific field where we have single character delimiter. Here we will see how can we fetch specific field where data is separated by multi character delimiter.

$ echo "1001***A1***HR***5000" | awk -F'***' '{print $1}' 

Output:
awk: There is a regular expression error.
        ?, *, or + not preceded by valid regular expression


$ echo "1001***A1***HR***5000" | awk -F'[*][*][*]' '{print $1}' 
Output:
1001

You can achieve the same by cut command as given below:

$ echo "1001***A1***HR***5000" | cut -d'***' -f1

Output:
1001

Keep Reading, Keep Learning, Keep Sharing...!!

Friday, 12 February 2016

How To Send Email In HTML Formatting In Unix/Linux

In This article we will see how to send an email notification with HTML formatting. We will see how to create and send following table type structure in an email body.


#----------------------------------------START OF THE SCRIPT--------------------------------------
$ cat Email_In_Html_Format.sh

#!/usr/bin/sh

V_MAIL_TO="Director@kw.com"
V_EMAIL_BODY="Email_Body.html"

#--Below code will be used to generate the HTML code
echo "<html>" > ${V_EMAIL_BODY}
echo "<head></head>" >> ${V_EMAIL_BODY}
echo "<body>" >> ${V_EMAIL_BODY}
echo "<STYLE TYPE="text/css">" >> ${V_EMAIL_BODY}
echo "<!--"  >> ${V_EMAIL_BODY}
echo "TD{font-family: Arial; font-size: 9pt;}" >> ${V_EMAIL_BODY}
echo "--->"  >> ${V_EMAIL_BODY}
echo "</STYLE>" >> ${V_EMAIL_BODY}

#--Below echo is used to create a HTML table
echo "<TABLE border="2" frame="border" rules="all">" >> ${V_EMAIL_BODY}

#--Below echo is used to create table header.
echo "<tr><th bgcolor=#BDBDBD><font size="2">EMP_ID</font></th>
<th bgcolor=#BDBDBD><font size="2">EMP_NAME</font></th>
<th bgcolor=#BDBDBD><font size="2">EMAP_DEPT</font></th>
<th bgcolor=#BDBDBD><font size="2">EMP_SAL</font></th></tr>" >> ${V_EMAIL_BODY}

#--Below echo is used to generate the data in tabular format.
echo "<tr><td>1001</td><td>A1</td><td>HR</td><td>50000</td></tr>          <tr><td>1002</td><td>A2</td><td>MAN</td><td>60000</td></tr>
      <tr><td>1003</td><td>A3</td><td>VP</td><td>100000</td></tr>         <tr><td>1003</td><td>A3</td><td>CEO</td><td>150000</td></tr>" 
      >> ${V_EMAIL_BODY}
echo "</table>" >> ${V_EMAIL_BODY}
echo "</body>" >> ${V_EMAIL_BODY}
echo "</html>" >> ${V_EMAIL_BODY}

#--Below code will be used to send email notification
export V_CONTENT="${V_EMAIL_BODY}"
(
echo "Subject: Employees Report"
echo "To: ${V_MAIL_TO}"
echo "MIME-Version: 1.0"
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
echo ${V_CONTENT}

) | /usr/sbin/sendmail ${V_MAIL_TO}

#-------------------------------------------END OF THE SCRIPT---------------------------------------

Output: Above Script will send the email to the user Director@kw.com with the tabular format. We can use such emails in notifying details with more clarification and readability.

Keep Reading, Keep Learning, Keep Sharing..!!!

How To Send Email In Unix

In This post we will see how to send email using mailx command available in most of the Unix like operating system.

SYNTAX: mailx <Switch> "<Subject>" <Email Of Address Recipient>

Email With subject

mailx -s "Test" "A1@kw.com"

Output: Will send an email to A1@kw.com with subject line as test
To:  A1@kw.com

Email With CC and BCC 

We can also use cc and bcc email option by using -c and -b

mailx -s "test" -c "A2@kw.com" -b "A3@kw.com" "A1@kw.com"

Output: Email recipients will appear as shown below in email. 
To:  A1@kw.com
Cc:  A2@kw.com
Bcc: A3@kw.com

Email With body

We can do that in multiple ways:

echo "Hello User" | mailx -s "test" "A1@kw.com"

Output:In this case, email body will have following text: "Hello User"
  
We can also use the content of a file as an email body, for example we have a file F_Msg_File.txt

cat F_Msg_File.txt
Hi User,

Please Note, We will be discussing mailx command.

Thanks
Knowledge Warehouse
   
(cat F_Msg_File.txt) | mailx -s "test" "A1@kw.com"

[OR]

mailx -s "test" "A1@kw.com" < F_Msg_File.txt

Output: Above command will send an email to the user with the file content as email body.

Email With Attachment

We can attach(log file or anything) an attachment in the email by using uuencode command.

(uuencode F_Log_File.txt F_Log_File.txt) | mailx -s "test" "A1@kw.com"

Email With Attachment & body

(cat F_Msg_File.txt; uuencode F_Error_File.txt F_Error_File.txt) | mailx -s "test" A1@kw.com

Output: Will attach F_Error_File.txt in email as attachment and display the content of F_Msg_File.txt as email body.

Note: Observe carefully, we have provided F_Log_File.txt wice in uuencode command.


Keep Reading, Keep Learning, Keep Sharing...!!

How To Use Alias In Unix/Linux

Many a times we face situation where we have to move in different directories to analyse the issue or checkout the multiple objects involved. At times it becomes very lengthy and time consuming as you have type a lot to change the directories involved.

For example, let say you have following directories available for your project:
  • Source File Directory which keeps the source file
  • Target File Directory which keeps the files generated after some processing.
  • Scripts, executable logics which perform some action
  • Log File Directory, keeps the log of all the activities
Now, if you have are doing some impact analysis it will become very time consuming to navigate in these directories. Alias command provides solution of such situations. How? Let see...!!

Source File Directory: /home/baba/kw/SourceFiles/ABC
Target File Directory: /home/baba/kw/TargetFiles/ABC
Script File Directory: /home/baba/kw/Scripts/ABC
Log    File Directory: /home/baba/kw/Logs/ABC

alias Source='cd /home/baba/kw/SourceFiles/ABC'
alias Target='cd /home/baba/kw/TargetFiles/ABC'
alias Script='cd /home/baba/kw/Scripts/ABC'
alias Log='cd /home/baba/kw/Logs/ABC'
  
Now if you type log at terminal, it will directly take you to the log directory, similarly if you want to go to source directory while you are still in log directory, just type source
you will be in source file directory.

alias command is active till the session is active. if you want it to be permanent declare it in user profile file (.profile  or bash profile)


It is not like you can only navigate in directory using alias, you can almost use it in all the repetitive work. For example, you want to count number of line in a file.

alias cnt='wc -l'

$ cnt F_Input_File

It will return you the number of line available in F_Input_File

alias clr='clear'

now by only typing c your screen will be cleared.

If you want to remove alias, use unalias command to remove the same.

unalias clr

will remove the alias clr.


Conclusion: You can create alias for almost all the day to day activity so utilize this feature to make your life ease....!!!

Keep Reading, Keep Learning, Keep Sharing...!!
Related Posts Plugin for WordPress, Blogger...