C.Dunham C.Dunham - 2 months ago 6
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:

2009-02-07T18:54:59.010Z,-5.878,146.884,34,6.3,mwc
2009-02-09T14:09:05.110Z,-5.902,146.967,35,5.8,mwc
2009-02-10T07:29:33.500Z,-22.82,-68.484,103,5.7,mwc


List B:

2009-02-09T14:09
2009-02-11T09:31
2009-02-11T13:52


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?

Cheers.

jas jas
Answer

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
2009-02-09T14:09:05.110Z,-5.902,146.967,35,5.8,mwc
Comments