-1

I am trying to compare these 2 files and only print out what I need as the desire output.

File1: 
012345:x:9012345:9012345:John Smith:/home/bin/bash
543210:x:9876543:9876543:Troy Denver:/home/bin/bash
111111:x:9898989:9898989:Mathew Moore:/home/bin/bash
222222:x:0101010:0101010:Chuck Maxwell:/homebin/bash
333333:x:1212121:1212121:Bob Evans:/home/bin/bash

File2:
333333 ALL=(ALL) NOPASSWD: ALL
543210 ALL=(ALL) NOPASSWD: ALL
222222 ALL=(ALL) NOPASSWD: ALL

Desired Output: 
333333 Bob Evans
543210 Troy Denver
222222 Chuck Maxwell

this is what I've done

readarray num1 < file1 | awk -F'[ ]' '{print $1}'
while read -r $num1;
        do grep "$num1" file2
        echo "$num1" | awk -F':' '{print $1,$5}'

done < file3.txt
LNL
  • 5
  • 2
  • 3
    It is hard to tell you where your problem(s) are because you did not show your code. Also see [How much research effort is expected of Stack Overflow users?](http://meta.stackoverflow.com/q/261592/608639) – jww Jan 15 '19 at 18:36
  • my bad, this is my first post. I just added the piece of code that I've done so far – LNL Jan 15 '19 at 19:06
  • Try https://stackoverflow.com/q/32481877/3220113 – Walter A Jan 15 '19 at 22:56
  • Try `while read -r firstfield otherfields; do ... done` – Walter A Jan 15 '19 at 23:00

1 Answers1

0

With GNU join, sort, sed and bash:

join -j 1 -t : <(sort File1) <(sed 's/ /:/' File2 | sort) -o 1.1,1.5 | sed 's/:/ /'

Output:

222222 Chuck Maxwell
333333 Bob Evans
543210 Troy Denver

See: man join

Cyrus
  • 84,225
  • 14
  • 89
  • 153