0

I m new to bash Shell scripting. I have a requirement that I have a lookup file (csv) in which there are set of strings in it like below.

file1:

text1
text2
text3

I have to check whether the strings in file1 are present in the file2.

file2:

s.no    desc
1       text5
2       text3
3       text2
4       text9

If the the string in file1 is there in file2, then I have to print the output in a new file file3 with the s.no and the string found. Please help.

5
  • Please add sample output for that sample input to your question. Commented Jun 11, 2017 at 6:11
  • my output file may be like S.no desc 3 text2 2 text3 Commented Jun 11, 2017 at 6:13
  • How are your columns separated? One Space, multiple spaces or one tab? Commented Jun 11, 2017 at 7:25
  • it's one tab separated Commented Jun 11, 2017 at 7:53
  • With join, sort and bash: join -1 1 -2 2 -t $'\t' --header <(echo; sort file1) <(sort -t $'\t' -k2,2 file2) -o 2.1,2.2 Commented Jun 11, 2017 at 8:18

1 Answer 1

1

Use

grep --file=file1 file2

or

grep -f file1 file2

From manual page of grep

  -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.)

Note: Add -w if you want only exact word match. So footext3bar will not be matched with text3.

Also of file1 contains regular expressing keywords like * or ^ then add -F also.

So updated command would be

grep -Fwf file1 file2

Example

bash-4.2$ cat file1
text1
text2
text3
bash-4.2$ cat file2
s.no    desc
1       text5
2       text3
3       text2
4       text9
bash-4.2$
bash-4.2$ grep --file=file1 file2
2       text3
3       text2
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Utsav. that's working perfectly. if my file2 has a value like below s.no desc 1 like text5 is xxxx 2 some text3 is qwe 3 random text2 is dfgg 4 call text9 has phdg how can i check whether the lookup string is there in the file2? let's say, this is kind of a error checking mechanism where in we are using a specific error keyword from file1 to search in a log file (file2).
Cool. Please read What should I do when someone answers my question so the question could be closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.