[LeetCode]#194. Transpose File
Dec 23, 2020
Environment: bash
Key technique: while, cat
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:
name age
alice 21
ryan 30
Output the following:
name alice ryan
age 21 30
Solution:
NUM_FIELDS=`cat file.txt | head -1 | awk '{ print NF}'`let count=1
while [ $count -le $NUM_FIELDS ]; do
result=`cat file.txt | awk -v x=$count '{print $x}'`
echo $resultlet count+=1
done
Submissions:
Reference:
https://leetcode.com/problems/transpose-file/discuss/699640/awk-simple-solution