0

I want to locate the error code (like 500,200,404) from the apache log file. the error code must be taken as input given by me, ( when I run the script it must prompt like

" please enter the error code here (404,500,200): ..... "

when I enter the error code, it must sort and show the word count of that particular error happened in that apache log. and ask me to specify a name to save the file.

I am new to script, what I tried is below.

read -p "Enter the error code : " CODE

 #echo 'Your Error code is,HTTP/1.1"' $CODE

 cat test.txt | grep 'HTTP/1.1" $CODE' | wc -l 

'HTTP/1.1" 500' or 'HTTP/1.1" 200' is the actual word I want to search, because if I grep 500 only, there will be more output. (like there is any other 500 is there it also pop up, I need only the 'HTTP/1.1" "error code" ' (error code which I mention as input while prompting)

melpomene
  • 84,125
  • 8
  • 85
  • 148
Sreeju KS
  • 39
  • 9
  • https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash – melpomene Aug 19 '19 at 06:33
  • `cat test.txt | grep "HTTP/1.1 $CODE" | wc -l` You must use double quotes and make sure you have the correct number of spaces between the HTTP/1.1 and the 500. – AAber Aug 19 '19 at 07:07
  • @sreejuks : Like AAber said, but if you don't know the number of spaces, respectively if this can vary, use a suitable regexp instead, for instance `... grep -E "HTTP/1.1 +$CODE"` – user1934428 Aug 19 '19 at 09:21
  • Or `grep "HTTP[/]1[.]1.*$CODE$"` – David C. Rankin Aug 19 '19 at 20:23
  • unfortunately double quotes not work here i think. There is also a double quote in the error code. this is the actual error code in the log HTTP/1.1" 500 <- [17/Aug/2019:23:48:00 +0500] "POST /path/path/ HTTP/1.1" 500 0000 "-" "-" 2019-08-17T23:48:00+05:00 app-server apache: 192.168.0.1 - - this is the entire log, from this i want to sort the 500 error only (the above log is edited due to security reasons) i can grep this using >--> grep 'HTTP/1.1" 500' | wc -l in terminal, but i cannot do this in script with a input specified unless i have to mention the error in the script itself. – Sreeju KS Aug 20 '19 at 05:21

0 Answers0