C.Dunham C.Dunham - 1 year ago 64
Linux Question

Editing the content of one list based on the content of a second list with limited columns

I am trying to delete all lines in List A that don't match lines in List B.

My issue is that the format of both lists is only identical for the first 16 columns.

I only want to compare the lists by these initial columns, but I want to keep the full contents of each matching line in List A.

Example contents of both lists:

List A:


List B:


My initial thought was to use grep -F to do this, but I'm not having any luck. Are there any ways to do this using awk or sed?


jas jas
Answer Source

This assumes lines in List A are unique with respect to the first 16 characters (or if not, that you only want to print the latest one). Also, if lines in List B are repeated, the output will repeat the line from List A, as well.

$ awk -F: 'NR==FNR{a[$1 $2]=$0; next} ($1 $2) in a {print a[$1 $2]}' a.txt b.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download