Grep multiple exact word


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep multiple exact word
# 1  
Old 06-03-2019
Grep multiple exact word

Hi
I am trying to grep multiple exact word from log file and directing it to a new file.
however my log file has many numeric values, such as 0400, 0401, 0404
and all html error also starts with 404, 401 etc
so I just want to grep only when 404, 401 etc is coming, NOT 0400, OR 0401
i have written below code, but when i run it takes all 0400 etc and 400, , i just need the line when only exactly 404 is coming
please help
Code:
find /home/bharat/ -type f -name "apache_logs.txt" |while read file
  do
    RESULT=$(egrep "400|401|403|404|405" $file)
      if [[ ! -z $RESULT ]]
         then
            echo "Error(s) in $file on $HOSTNAME at "$(date)": $RESULT">> email_result.txt 
     fi
  done

Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags.
it makes it far easier to read and preserves spaces for indenting or fixed-width data.

Last edited by rbatte1; 06-04-2019 at 07:53 AM..
# 2  
Old 06-03-2019
man grep yields:
Code:
       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings (instead of regular
              expressions), separated by newlines,  any  of  which  is  to  be
              matched.

# 3  
Old 06-04-2019
Please use code tags.
Try
Code:
if egrep "[^0](400|401|403|404|405)" file; then echo errors; fi

This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-04-2019
Code:
egrep "\<(400|401|403|404|405)\>" $file

# 5  
Old 06-04-2019
Why not use an expression like [^0]40[01345]" with appropriate wrapping to make them whole 'words' if needed. Can you share some of the data you want to work through and highlight in green the ones you want to match, with red for those you want to avoid?




Thanks, in advance,
Robin
# 6  
Old 06-04-2019
Try:
Code:
grep -w '40[01345]' file

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 06-05-2019
Also
Code:
grep -w -e 400 -e 401 -e 403 -e 404 -e 405 file

or
Code:
grep -w '400
401
403
404
405' file

might work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep exact math word

Hi All, i want exact math to search to find it and i tried as like below it not working. My Excepted out : should not get the output that mean exact word math. echo "test.txt|123"|sed 's/|/ /g'|grep -w "test" Thanks (1 Reply)
Discussion started by: bmk123
1 Replies

2. Linux

How to search multiple word using grep command?

How to search multiple word using grep command for example i want to reserch ANJ001 AA Using ridiculous font, size, and color changes instead of normal space separated text and CODE tags obfuscates what you are trying to do and makes it difficult for volunteers who may want to help you solve... (1 Reply)
Discussion started by: na.dharma
1 Replies

3. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

4. Shell Programming and Scripting

Grep Exact word

This may be stupid question but not able to solve it. How to grep exact word and line along with it. TEST:/u00/app/oracle/product/10.2.0/TEST:N TEST2:/u00/app/oracle/product/10.2.0/ODS:N TEST3:/u00/app/oracle/product/10.2.0/TEST:N TEST4:/u00/app/oracle/product/10.2.0/ODS:N... (4 Replies)
Discussion started by: tapia
4 Replies

5. UNIX for Dummies Questions & Answers

How to grep the exact string / word ?

Hi All, I have a text / log file which contains strings like meta777, 77, meta, 777. Now I want to write a script which can detect a string 'meta#777' in a text file & number of occurence of 'meta', number of #, number 7, 77, 777. I'm using grep -e '77' filename but no luck. It is returning... (5 Replies)
Discussion started by: adc22
5 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

7. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

8. Shell Programming and Scripting

grep multiple patterns + the whole word only

Guys, i used egrep "pattern1|pattern2". But the whole word is searched. But i want the output if only the exact word is matched. i.e the output is got evenif a part of the pattern is matched. I tried the -w opion but its showing usage error. Please help me out on this one. please sent me... (2 Replies)
Discussion started by: meheretoknow
2 Replies

9. Linux

option of grep for counting exact word ??

Hi All, I have a quary regarding grep command in linux. I have a file which contains 56677 56677 +56677 +56677 56677 56677 56677 I want to extract total count of "56677" When I hit the following command #cat filename | grep -w -c '56677' the result comes 7. Its counting... (3 Replies)
Discussion started by: maddy
3 Replies

10. Shell Programming and Scripting

grep for "exact word"

Hi All, Very GM I am searching for a specific filesystem on a serevr, like df -k "/" i am geting an output also... but when i am checking for somthing like /oramnt (which is not mounted currently) so i am geting an out like this df -k "/oramnt" output of root...! so i tried... (7 Replies)
Discussion started by: bullz26
7 Replies
Login or Register to Ask a Question