-1

what i'm trying is, reading all the strings from one file which also include special characters ie. ,/\'" etc. and i'm doing cat of one file with grep above string to check occurrence but it's not working for some special strings.

file1.txt

Error:'abcd' not found; Try Again
Error:load() loading provider library version 3.5(r182) By <info@xyz.com>
Warning: Connection to group 'xyz-xyz'

Filename.log

2017-06-19 06:50:28 Error:'abcd' not found; Try Again
2017-06-19 06:50:28 Error:load() loading provider library version 3.5(r182) By <info@xyz.com>
2017-06-19 06:50:28 Warning: Connection to group 'xyz-xyz', peer '00.00.01.01:2200,00.00.01.01:2200,00.00.01.01:2200' 

inside script test1.sh

while read STRING

do

cat Filename.log | grep -i -F "$STRING"

done<file1.txt

but it reads it as

grep -i -F 'Error:'\''abcd'\'' not found; Try Again'

And does not show any correct output.

So how can we ignore all the special characters inside this ? Other strings without any special characters are working fine.

Thanks

  • special characters includes : ' () <> @ # $ ? ~ – user2710412 Jun 30 '17 at 14:47
  • @anubhava, its a error log file , usually contains error messages such as above one "Error:'abcd' not found; Try Again" with date and time, other includes ip address and email id. Only concern is special character inside string which i want to filter. – user2710412 Jun 30 '17 at 14:49
  • 1
    Try `grep -i -F file1.txt Filename.log` Question is very unclear. Please provide example input and expected output. – Fredrik Pihl Jun 30 '17 at 14:49
  • Try to use `tr`: `cat Filename.log | tr "[:punct:]" " " | grep -i -F "$STRING"` – zombic Jun 30 '17 at 14:54
  • Find some inspiration [here](https://stackoverflow.com/questions/3337936/remove-non-ascii-characters-from-csv/3337960#3337960) and [here](https://stackoverflow.com/questions/36926999/removing-all-special-characters-in-bash) – Fredrik Pihl Jun 30 '17 at 14:56
  • `-F` doesn't interpret special characters... i.e `string=".*";grep -F "$string" `doesn't match everything. – 123 Jun 30 '17 at 15:01
  • i added some more information, hope it is better now – user2710412 Jun 30 '17 at 15:03
  • Works for me... – 123 Jun 30 '17 at 15:05

2 Answers2

0

You can use tr and replace all punctuation characters if they are not needed

...
while read STRING
do
cat Filename.log | tr "[:punct:]" " " | grep -i -F "$STRING"
done<file1.txt

Or | tr "\\\',/" " " for another characters.

zombic
  • 221
  • 1
  • 9
  • That's one approach, but again we'll have dependency on special characters we are passing on script. can we have any way by which we can pass whole string under double quotes " ... " – user2710412 Jun 30 '17 at 15:11
  • You can replace `| tr "'" "\'"` and escape symbol `' ` - `Error:\'abcd\' not found;` – zombic Jun 30 '17 at 15:44
0

Is this what you are after, using

-f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)

$ cat file1.txt 
Error:'abcd' not found; Try Again
Error:load() loading provider library version 3.5(r182) By <info@xyz.com>
Warning: Connection to group 'xyz-xyz'

$ cat Filename.log 
2017-06-19 06:50:28 Error:'abcd' not found; Try Again
2017-06-19 06:50:28 Error:load() loading provider library version 3.5(r182) By <info@xyz.com>
2017-06-19 06:50:28 Warning: Connection to group 'xyz-xyz', peer '00.00.01.01:2200,00.00.01.01:2200,00.00.01.01:2200'

$ grep -i -f file1.txt Filename.log 
2017-06-19 06:50:28 Error:'abcd' not found; Try Again
2017-06-19 06:50:28 Error:load() loading provider library version 3.5(r182) By <info@xyz.com>
2017-06-19 06:50:28 Warning: Connection to group 'xyz-xyz', peer '00.00.01.01:2200,00.00.01.01:2200,00.00.01.01:2200'

enter image description here

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Thanks Fredrik it is working great for above case, but there is one case this is not working for me , if file1.txt string is starting with any special character.. ie. [Error]:'abcd' not founf;........ – user2710412 Jun 30 '17 at 18:17
  • Again, works for me (tm), I'm using `grep (GNU grep) 2.20` – Fredrik Pihl Jun 30 '17 at 18:21