Search a string and send the line containing it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search a string and send the line containing it
# 1  
Old 11-01-2012
Reference 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
# 2  
Old 11-01-2012
Code:
grep "3456" abc.log | mailx -s "Error" <emailid>

# 3  
Old 11-01-2012
Quote:
Originally Posted by bipinajith
Code:
grep -w "3456" abc.log | mailx -s "Error" <emailid>

Slight modification: Adding word boundary...
# 4  
Old 11-01-2012
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.
# 5  
Old 11-01-2012
If you want line as subject:-

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

# 6  
Old 11-01-2012
below code will send the mail along with error attachment
Code:
 
grep -i '3456'  abc.log > error_line.txt

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

# 7  
Old 11-01-2012
Quote:
Originally Posted by only4satish
below code will send the mail along with error attachment
Code:
 
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Search the string in the active log and send mail

Hello, I wanted to search specific string in the acitve log file and send an email if the search string found in the log. Log file is written by application all the time. So, script has to search if any new log entry has the specific string for example " sample exception" and send an email. (1 Reply)
Discussion started by: balareddy
1 Replies

3. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

6. UNIX for Dummies Questions & Answers

Search string and send email

Hi All, I need script which should search for the "reason code" and it should send mail with its preceding line as well as the line below it.(first line before it and line after it) I used below script but it is printing the whole lines inside it, your help is appreciated: cd... (3 Replies)
Discussion started by: rockingvj
3 Replies

7. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

8. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

9. Shell Programming and Scripting

Search for string and send mail

Hi, I need to find if there is any error in the last few lines of the log file and send a mail accordingly.For example, Following errors can be logged in the log file. ERR_1="DB Connection not established" ERR_2="Server Unloading" I need to find if these errors are found in the log file and... (1 Reply)
Discussion started by: AnneAnne
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question