Search a string and send the line containing it

Folks,

Need a script which searches for a string and send line containing the string to the mail.

The requirement is like this :

file abc.log containts below text :

"error occurred due to the code 3456 and kill the process to recover it"

  1. search for string "3456" and send entire line "error occurred due to the code 3456 and kill the process to recover it" to my email id.

Any help !!!

Thanks

grep "3456" abc.log | mailx -s "Error" <emailid>

Slight modification: Adding word boundary...

Hello,

this will mail with subject error that's it and I need entire line containing the string and in mail i need the line to be displayed.

If you want line as subject:-

mailx -s "`grep -w 3456 abc.log`" <email> < /dev/null

below code will send the mail along with error attachment

 
grep -i '3456'  abc.log > error_line.txt

( cat error_line.txt; ) | mailx -s " ERROR"  'mailid'
 

This statement is not correct. Using this command you will simply get the content of this file as email body not as attachment.

thanks for correcting.............
how to add an attachment(.txt file) and mail it ...?

uuencode error_line.txt error_line.txt | mailx -m -s "ERROR" <email id>
uuencode <testfile.txt> <testfile.txt > | mailx -m -s "subjectmessage"  abc@gmail.com

is testfile.txt to be places twice after uuencode ?

Yes, 2 parameters required:-

uuencode <source file> <output file>

In addition to uuencode there's also sendmail command which is quite flexible and customized command..

uuencode <source file> <output file>

[/HTML]

i did not understand it ? source and output ???

First it will add the header & then the data is encoded for transmission over mail system.

uuencode error_line.txt  | mailx -m -s "ERROR" <email id>

if i want to attach the file error_line.txt while sending mail, will it work ??