LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   expected results not produced by grep (https://www.linuxquestions.org/questions/linux-newbie-8/expected-results-not-produced-by-grep-4175571533/)

dthims 02-05-2016 11:47 AM

expected results not produced by grep
 
cat /var/log/mail.log |grep 'connect from unknown[192.168.1.10]' - comes up empty
cat /var/log/mail.log |grep 'connect from unknown' - this works, but too many to sift thru
tia

rknichols 02-05-2016 12:03 PM

Quote:

Originally Posted by dthims (Post 5495751)
cat /var/log/mail.log |grep 'connect from unknown[192.168.1.10]'

That is looking for the string "connect from unknown" immediately (with no intervening space) followed by one of the characters from the set "012689." . The square brackets and "." are all special to grep. You need to either escape them with a backslash or use the "-F" (--fixed-strings) option in grep.
Code:

cat /var/log/mail.log |grep 'connect from unknown \[192\.168\.1\.10\]'
# or
cat /var/log/mail.log | grep -F 'connect from unknown [192.168.1.10]'


dthims 02-05-2016 12:16 PM

thnx so much. it was perfect

grail 02-05-2016 01:11 PM

Grep is also designed to read a file directly, so you can ditch cat too :)
Code:

grep -F 'connect from unknown [192.168.1.10]' /var/log/mail.log


All times are GMT -5. The time now is 06:14 AM.