Print only match pattern

Hi,

I want to print the lines from file1 which has the matching pattern from file2 in linux.

file1 data is below

-----------------
CACA|1234
CA|2345

file2 data is below

-----------------
CA
NC
TX
 

Now I want to print the lines from file1 for the values present in file2.

  1. print the string which occurs more than once. In this case the out i need to get is CACA|1234

  2. Print the string which occurs only once. in this case the output i need to get is CA|2345

can any one help me how to do this please

For starters

grep - o '[pattern]'  file 

prints just the pattern, nothing else -- GNU grep only, which normally is part of a Linux distribution.

thanks for the reply. but i need to print the lines

 
grep -f file2 file1

This will help you print lines from file1 that matches patterns in file2

awk -F"|" 'FNR==NR{a[$1];next}$1 in a' file2 file1

hi,

there are two cases here.

  1. print the string which occurs more than once into seperate file
  2. Print the string which occurs only once into seperate file

so need to capture the output into seperate files.

the above solutions all are capturing either one or more occurences of string to a single file.