1

I have a file index.txt with data like:

2013/10/13-121 f19f26f09691c2429cb33456cf64f867
2013/10/17-131 583d3936c814c1bf4e663fe1688fe4a3
2013/10/20-106 0f7082e2bb7224aad0bd7a6401532f56
2013/10/10-129 33f7592a4ad22f9f6d63d6a17782d023
......

And a second file in CSV format with data like:

2013/10/13-121, DLDFWSXDR, 15:33, 18:21, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965
2013/10/17-131, DLDFWXDR, 11:05, 15:08, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965
2013/10/20-106, DLSDXDR, 12:08, 13:06, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965
2013/10/10-129, DLXDAE, 15:33, 18:46, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965

Now i need a solution to add the MD5SUM at the end of the CSV when the ID (first column) is a match in the index file or vice versa , so at the end the file should look like this:

2013/10/13-121, DLDFWSXDR, 15:33, 18:21, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, f19f26f09691c2429cb33456cf64f867
2013/10/17-131, DLDFWXDR, 11:05, 15:08, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 583d3936c814c1bf4e663fe1688fe4a3
2013/10/20-106, DLSDXDR, 12:08, 13:06, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 0f7082e2bb7224aad0bd7a6401532f56
2013/10/10-129, DLXDAE, 15:33, 18:46, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 33f7592a4ad22f9f6d63d6a17782d023

3 Answers 3

1

Try with join command.

join  <(sort file1.csv | sed 's/$/,/g') <(sort index.txt | sed 's/ /, /g')

2013/10/10-129, DLXDAE, 15:33, 18:46, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 33f7592a4ad22f9f6d63d6a17782d023
2013/10/13-121, DLDFWSXDR, 15:33, 18:21, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, f19f26f09691c2429cb33456cf64f867
2013/10/17-131, DLDFWXDR, 11:05, 15:08, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 583d3936c814c1bf4e663fe1688fe4a3
2013/10/20-106, DLSDXDR, 12:08, 13:06, Normal, No, 5F2B0121-4F2B-481D-B79F-2DC827B85093/22551965, 0f7082e2bb7224aad0bd7a6401532f56
  • sort t1 | sed 's/$/,/g' sort file1.csv and sufix it by comma.
  • sort t2 | sed 's/ /, /g' sort index.txt and replace the delimitor
  • Then use join command (by default 1st field).
2
  • Brilliant if IDs and their count are identical; what if they differ?
    – RudiC
    Aug 23, 2018 at 12:04
  • Thanks RudiC, thats exactly the point, the Index file i have has 11785 uniq IDs and the csv has 139898 lines with multiple same IDs.
    – T-One
    Aug 24, 2018 at 11:11
1

Using awk without sorting any file:

awk 'NR==FNR{a[$1]=$2;next}{$(NF+1)=a[$1]}1' index.txt FS=', ' OFS=', ' file.csv

The first block statement allows the first file index.txt to be copied into the array a.

The second block statement append the content of the array a at the end of the line of file file.csv if the key (first field of the file) is part of the array.

1

Try also - should IDs in first and second file differ - :

awk 'NR == FNR {S[$1] = $2; next} {print $0 FS " " S[$1]}' file1 FS="," file2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .