-1

File1.txt

phone_number

0777
0788
0789
0766
0756

File2.txt

phone_number name address

0777 Joe street1
0788 Karen street2
0789 Dave street3
0783 Sean street4
0781 Mick street5

Output required

phone_number name address

0777 Joe street1
0788 Karen street2
0789 Dave street3

Tried comm file1.txt file2.txt >comm2.txt

But the output shows there is no matching lines, because of the extra columns in file 2.txt.

Looked at a few awk scripts also but no joy getting desired output (common lines in each file with file2.txt extra columns included).

danc90
  • 1
  • 1
  • Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Jul 19 '18 at 10:50
  • @jww I'm not working on a script. The problem is I don't know the commands to use to get my desired output! Which is the common lines within file 1 and 2 but file 2 has more columns and I don't want to suppress those columns in the output. Thought that was pretty obvious – danc90 Jul 19 '18 at 11:01

1 Answers1

1
join -1 1 -2 1 <(sort File1.txt) <(sort File2.txt)

join will join the data on the specified field, but require that the input be sorted.

Therefore, the sorted result is passed to join.

Due to the sorting process, the header may be an issue, but you can remove it and then put it back later.

dibery
  • 2,760
  • 4
  • 16
  • 25
  • Tried this and sent output to a new txt file, however only the column headers appeared in the file. When removed and command re-ran the output was blank. I have 7 columns in file2.txt which follow phonenumber column @dibery? – danc90 Jul 19 '18 at 13:36
  • It would be good if you post all fields or part of your real data. I checked with the input in your statement and the output matched yours. – dibery Jul 19 '18 at 14:00
  • Hmm, the above command still works on my machine. Are the fields separated by space? Is there any space between the `<()` above? – dibery Jul 19 '18 at 15:08
  • Its difficult to post the data heading here in comments. The fields are seperated by spaces yes – danc90 Jul 20 '18 at 10:08
  • There should be no space between the `<(`, and you can add some real data by clicking the edit button below your question. – dibery Jul 21 '18 at 01:58