Problem
Given a text file file.txt, transpose its content.
You may assume that each row has the same number of columns and each field is separated by the ' ' character.
Example:
If file.txt has the following content:
1 | |
Output the following:
1 | |
Explanation
-
We can use
awkto solve this problem. In awk,NFmeans the number of column and starts from 1.NRmeans the number of row and also starts from 1. -
In the above example,
NFis counting like 1, 2, 1, 2, 1, 2 since we have three rows and two columns in each row. -
In iteration, if it’s the first row, then we create a new array element
s[i] = $i; else, we append$itos[i].
Solution
1 | |