Sample Header Ad - 728x90

Concatenating columns of the same csv file to create a new column with a new heading

0 votes
2 answers
2262 views
What I have is a CSV file to this effect: +------------+--------------+ | Category I | Sub-Category | +------------+--------------+ | 1144 | 128 | | 1144 | 128 | | 1000 | 100 | | 1001 | 100 | | 1002 | 100 | | 1002 | 100 | | 1011 | 102 | | 1011 | 102 | | 1011 | 102 | | 1011 | 102 | | 1011 | 102 | | 1011 | 102 | | 1013 | 103 | | 1013 | 103 | | 1013 | 103 | | 1013 | 103 | | 1013 | 103 | | 1013 | 103 | | 1013 | 103 | +------------+--------------+ I wish to concatenate the first and second columns above to form a third, new column with a new arbitrary heading, to this effect: +-------------+--------------+-----------------------+ | Category ID | Sub-Category | Arbitrary New Heading | +-------------+--------------+-----------------------+ | 1144 | 128 | 1144128 | | 1144 | 128 | 1144128 | | 1000 | 100 | 1000100 | | 1001 | 100 | 1001100 | | 1002 | 100 | 1002100 | | 1002 | 100 | 1002100 | | 1011 | 102 | 1011102 | | 1011 | 102 | 1011102 | | 1011 | 102 | 1011102 | | 1011 | 102 | 1011102 | | 1011 | 102 | 1011102 | | 1011 | 102 | 1011102 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | | 1013 | 103 | 1013103 | +-------------+--------------+-----------------------+ My usual go-to utility, csvkit does not have the means to achieve this, afaik - see https://github.com/wireservice/csvkit/issues/930 . What is a simple solution not requiring advanced programming knowledge, which can achieve this? I'm vaguely aware of awk and sed as potential solutions, but I don't want to limit the enquiry to those just in case there is a better (i.e. simpler) solution. The solution must be efficient for very large files, i.e containing 120,000+ lines. Edit: I have included the sample data for the convenience of those wanting to take a crack at it; download here: https://www.dropbox.com/s/achtyxg7qi1629k/category-subcat-test.csv?dl=0
Asked by ptrcao (5995 rep)
Dec 21, 2019, 09:56 AM
Last activity: Dec 25, 2019, 06:24 AM