LeetCode刷题实战194:转置文件
程序IT圈
共 1168字,需浏览 3分钟
·
2021-02-26 14:07
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.
题意
示例
示例:
假设 file.txt 文件内容如下:
name age
alice 21
ryan 30
应当输出:
name alice ryan
age 21 30
解题
思路:先用awk获取列数,再循环
k=`awk '{print NF}' file.txt | head -1`
for ((i=1;i<=k;i++))
do
awk '{print $'$i'}' file.txt | xargs
done
评论