-1

Given file 1...

2/26a- 6
23/26a- 20z
23/26A-18

and file 2...

2/26a-6
23/26a- 20Z
23/26A-18
25/2a -18
22/34B-19

How can I search file 2 for the contents of file 1 ? Here the output should be:

2/26a-6
23/26a- 20Z
23/26A-18

(So not an exact match, but there is some shared content). Matching should be case-insensitive.

John WH Smith
  • 15,880

2 Answers2

1

Here you are:

tr -d " \t\r" < file1 > X

tr -d " \t\r" < file2 > Z

And then:

grep --ignore-case -Fxf X Z

Done!

1

You could modify the contents of file1 to replace sequences of one or more whitespace characters with a regular expression representing zero or more whitespace characters:

sed 's/[[:space:]]\{1,\}/[[:space:]]*/g' file1
2/26a-[[:space:]]*6
23/26a-[[:space:]]*20z
23/26A-18

and then use that as a pattern file for your case-insensitive grep:

sed 's/[[:space:]]\{1,\}/[[:space:]]*/g' file1 | grep -if- file2
2/26a-6
23/26a- 20Z
23/26A-18

NOTE: this approach will only work if the entries in your file1 do not contain other special characters - if they do, then these will need to be suitably escaped.

steeldriver
  • 81,074
  • Thanks, very close but issue here is that file 2 also has spaces in some of the names. – user183218 Aug 07 '16 at 15:21
  • Actually i am getting some of the names from file2 ? I have 180 entries in file1 and 4500 in file2. so the output should be 180 (similar to file 1) from file 2. – user183218 Aug 07 '16 at 15:39
  • @user183218 it should handle spaces in the names in file2 - for example it is clearly matching 23/26a- 20Z. If you want further help then please provide a more representative sample of your files. – steeldriver Aug 07 '16 at 16:06
  • Actually it is long list having 5000 entries. How to send it ? Pl suggest. – user183218 Aug 13 '16 at 06:55