LeetCode194——转置文件

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/86636476

我的LeetCode代码仓:https://github.com/617076674/LeetCode

原题链接:https://leetcode-cn.com/problems/transpose-file/description/

题目描述:

知识点:Linux常用指令

思路:awk命令

NR是awk命令的内建变量,表示已经读出的记录数,就是行号,从1开始。

注意:连接字符串时无需用加号。

Bash脚本

awk '
{
    for (i = 1; i <= NF; i++) {
        if(NR == 1) {
            s[i] = $i;
        } else {
            s[i] = s[i]" "$i;
        }
    }
}
END {
    for (i = 1; s[i] != ""; i++) {
        print s[i];
    }
}' file.txt

LeetCode解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/86636476