brucezepplin
brucezepplin

Reputation: 9782

grep matches between two files and convert to lower case

I need a fast and efficient approach to the following problem (I am working with many files.) But for example:

I have two files: file2

Hello
Goodbye
Salut
Bonjour

and file1

Hello, is it Me you're looking for?

I would like to find any word in file 2 that exists in file 2, and then convert that word to lower case.

I can grep the words in a file by doing:

grep -f file2.txt file1.txt

and returns

Hello

So now I want to convert to

hello

so that the final output is

hello, is it Me you're looking for?

Where if I match multiple files:

grep -f file2.txt *_infile.txt

The output will be stored in respective separate outfiles.

I know I can convert to lower case using something like tr, but I only know how to do this on every instance of an uppercase letter. I only want to convert words common between two files from uppercase to lowercase.

Thanks.

Upvotes: 1

Views: 538

Answers (1)

user3159253
user3159253

Reputation: 17455

I would solve the problem a bit differently.

First, I would mark matches in grep. --color=always works well, although it's somewhat cumbersome and potentially unreliable in detection. Then I would change marked matches with sed or perl:

grep --color=always -F -f file2.txt file1.txt | \
    perl -p -e  's/\x1b.*?\[K(.*?)\x1b.*?\[K/\L\1/g'

The cryptic RE matches the coloring escape sequence before the match, de-coloring escape sequence right after the match and captures everything in between into group 1. Then it applies lowercase \L conversion to the capture. Likely GNU sed can do the same, but probably perl is more portable.

Upvotes: 1

Related Questions