0

File1 contains numbers each per line (0 to 9 digit length) total records 20k File2 contains numbers(0 to 9 digit) in first column and other info in each line,total recs 46 ,000,00 Both files are sorted. Need to grep all recs from file2 which have common entries in file1

File1

988676562 

99373

133838000

File2

99373        dhdhdhdhd, 3838 dheueie,aaaa

133838000  rrtyusbjsjs,382 djdjdjsusu

2233747      Eheueueu. ,446,ttttiieenjs

44577333  Euedjdbebe,777,rrididjd

Output

99373         dhdhdhdhd, 3838 dheueie,aaaa

133838000  rrtyusbjsjs,382 djdjdjsusu

I tried fgrep -f file1 file2 but the output is nil

Let me clarify first column are exactoy same in both the files

6
  • 1
    In my case grep -f file1 file2 is working.
    – Prvt_Yadav
    Mar 22, 2019 at 6:56
  • 1
    Are those dots at the end of the first column's data actually there?
    – Kusalananda
    Mar 22, 2019 at 7:34
  • @prvt_yadav add 77 to your keys file and watch it match 44577333.
    – roaima
    Mar 22, 2019 at 8:02
  • Yes first column in both the files are same. There is jo dot at the end of numbers.
    – kcmakwana
    Mar 22, 2019 at 8:50
  • I think, 2nd file is having 46 lacs records thats why grep is not working?
    – kcmakwana
    Mar 22, 2019 at 8:51

4 Answers 4

0

I'm not sure if your File1 contains empty lines. That's why I make sure lines without numbers get ignored. Furthermore your sample File1 seems to contain trailing spaces. That's way I will remove all leading and trailing non-digit characters.

You could use sed to create patterns for grep from this file and use the result as a pattern file to grep in File2.

grep -f <(grep '[0-9]' File1 | sed 's/[^0-9]*\([0-9]*\).*/^\1[^0-9]*.*$/' ) File2

This prints

99373         dhdhdhdhd, 3838 dheueie,aaaa
133838000  rrtyusbjsjs,382 djdjdjsusu

Explanation:

Extract lines that contain a number

grep '[0-9]' File1

Remove possible leading and trailing garbage and create a pattern for grep to find the nunber at the beginning of the line followed by a non-number character and anything else or by nothing up to the end of the line.

sed 's/[^0-9]*\([0-9]*\).*/^[^0-9]*\1[^0-9]*.*$/'
0

For this kind of job, join is very useful. While using join keep in mind that both file are sorted on column, which are identical in both files.

Here is the solution

sort -o file1 file1 #sort and save content of file1
sort -o file2 file2 #sort and save content of file2
join file1 file2
0

One possible solution (checking the empty lines):

while read line; do [ ${#line} -gt 0 ] && grep "$line" File2; done < File1
0

Use below command tested and it worked fine

command

praveen@praveen:~$ awk 'NR==FNR{a[$1];next}($1 in a){print $0}' file1 file2

output

praveen@praveen:~$ awk 'NR==FNR{a[$1];next}($1 in a){print $0}' file1 file2

99373        dhdhdhdhd, 3838 dheueie,aaaa

133838000  rrtyusbjsjs,382 djdjdjsusu

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .