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
awk
to solve this problem. In awk,NF
means the number of column and starts from 1.NR
means the number of row and also starts from 1. -
In the above example,
NF
is 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$i
tos[i]
.
Solution
1 |
|