Awk – replace coumn 2 in table 1 from coumn 2 in table 2 based on matching data in column 1 (common between tables)

#!/usr/bin/env python
with open('/etc/shadow','rb') as file:
  for line in file:
    TargetLine = line.rstrip().split(":")
    with open('shadow.sync','rb') as shadow:
      for row in shadow:
        SyncLine = row.rstrip().split(":")
        if TargetLine[0] == SyncLine[0]:
          TargetLine[1] = SyncLine[1]
          break
    print "NEW MODIFIED LINE: %s" % ":".join(TargetLine)

This will open the /etc/shadow file and loop through the lines. For each line on the /etc/shadow file we loop through the shadow.sync file once a match for the usernames TargetLine[0] == SyncLine[0] the password field is modified and the loop is broken.
If a match is NOT found (username in /etc/shadow but NOT in the shadow.sync file) the if block on the inner loop falls through and the line is left untouched the results are handled on the final print statement. As this answers the question I will leave the data output and file manipulation up to the user.

Leave a Comment