I have a tab delimited file, in which the first 10 columns are always present and stay at same position, but after the 10th column, the number of columns can change. I want to retain the first 10 columns as is and concatenate the rest of the columns into one column separated by |.
Here is the sample data:
columns: col1 col2 … col 10 col11 col12 col13
Values: 1 2 … 10 11 12 13
Result:
Columns: col1 col2 … col10 col11|col12|col13
Values: 1 2 … 10 11 | 12 | 13
Take a look at ruby’s split method – it takes an optional
limitparameter. Assuming your data is in a variablerowfor each line:Example (with the variable part starting at column 3 for brevity):
Then you can change the last element in place: