0

I have a big file like this:

79597700
79000364
79002794
79002947

And other big file like this:

79597708|11
79000364|12
79002794|11
79002947|12
79002940|12

Then i need the numbers that appear in the second file that are in the first file bur with the second column, something like:

79000364|12
79002794|11
79002947|12
79002940|12

(The MSISDN that appear in the first file and appear in the second file, but i need return the two columns of the second file)

Who can help me, because whit a grep does not work to me because return only the MSISDN without the second column and with a comm is not possible because each row is different in the files

Code Geas Coder
  • 1,839
  • 4
  • 23
  • 29

2 Answers2

2

Try this:

grep -f bigfile1 bigfile2
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    Beware that if `bigfile1` contains a line containing just `3`, it will match a lot of lines that aren't wanted. You might need to do something fancy with process substitution (`grep -f <( sed 's/.*/^&|/' bigfile1) bigfile2`) to ensure that the number in file 1 exactly matches the whole of the first field in file 2. – Jonathan Leffler Jun 25 '15 at 21:19
  • Yes, that's right. A workaround: `sed -n -f <(sed -n 's|.*|/^&\|/p|p' bigfile1) bigfile2` – Cyrus Jun 25 '15 at 21:22
  • @JonathanLeffler `grep -x` will fix that. – o11c Jun 25 '15 at 22:31
  • @o11c: please explain how `grep -x` will help. – Jonathan Leffler Jun 25 '15 at 22:34
  • @JonathanLeffler it only matches whole lines ... wait, that won't handle the `|10` bit. Hm, `grep -w` maybe? – o11c Jun 25 '15 at 22:36
  • @o11c: Yup; `grep -x` wouldn't handle the pipe. You could use `grep -w` and as long as the numbers after the pipe aren't confusable with the numbers before, it will be fine. – Jonathan Leffler Jun 25 '15 at 22:49
1

Using awk:

awk -F"|" 'FNR==NR{f[$0];next}($1 in f)' file file2

Source: return common fields in two files

Community
  • 1
  • 1
Rakholiya Jenish
  • 3,165
  • 18
  • 28