There are about a text file with 1200 columns with different length.Please kindly note that, in this file, the header name of columns, except for the first column (S1 column) is identical with the contents within the first column (A, B, C, D, E, F). something like below:

S1  A   B   C   D   E   F
A   A   I   C   D   E   F
B   B   B   K       D   L
C   F   E           A   E
D   H               C   N

I would like to compare the first column (S1) with the rest of columns and find identical items between the first column and column 2, between the first column and column 3, and so on. Then, print the identical items in the new column (here, column O). The desired output is like below:

S1  A   B   C   D   E   F   O
A   A   I   C   D   E   F   A,B,F
B   B   B   K       D   L   B,E
C   F   E           A   E   C
D   H               C   N   D
E                           E,D,A,C
F                           F,E 

Here, the O column is the desired output column, which the identical names between S1 column and the second column (column A) is printed in the front of A in the column S1; similarly, the identical names between S1 column and the third column (column B) is printed in the front of B in the column S1, and so on.

  • Is there some other way to describe the construction of the O column? I don't understand why the B is there on the 2nd line, or E on the 3rd, or any of the other entries of that column on the remaining lines.
    – Kusalananda
    Apr 24, 2019 at 10:08
  • 1
    Shouldn't the first output not be A,B,F instead of A,B? @Kusalananda, remove the first row (header), take 2nd column A,B,F,H and find all matching letters in first column -> A,B,F, take 3rd column I,B,E + search in 1st column -> B,E, ...
    – Freddy
    Apr 24, 2019 at 10:29
  • Having the data in rows rather than columns would have made the matching bit easier.
    – Kusalananda
    Apr 24, 2019 at 10:51
  • 1
    In the row C F E A E C how can you tell that the 3rd and 4th columns are empty? Are the contents of each column exactly one character? Or are there exactly three blanks between each column and a blank column is one blank character? Apr 25, 2019 at 3:47
  • 1
    Your question says that columns have a different length, now you say that each column is 8 characters. Please edit the question to give a definition of what is a column. Usually table files either have a fixed delimiter between columns (like csv) or a fixed column width. If neither is true, it may be obvious to a human reader to understand what is supposed to be a column, but almost impossible to express as an algorithm to implement. Is there a chance to change the output of the other tool?
    – Philippos
    Apr 26, 2019 at 12:41


You must log in to answer this question.

Browse other questions tagged .