leetcode-shell-194. Transpose File
2016-06-16 16:04
483 查看
Given a text file
You may assume that each row has the same number of columns and each field is separated by the
For example, if
Output the following:
name alice ryan
age 21 30
题意:行列互换
思路:没有思路,看了别人使用awk完成的,然后自己写了一遍,感觉awk好强大
最终shell脚本:
#! /bin/sh
awk '
{
for(i=1;i<=NF;i++)
{
a[NR,i]=$i
}
}
END {
for(j=1;j<=NF;j++)
{
str=a[1,j]
for(i=2;i<=NR;i++)
{
str=str" "a[i,j]
}
print str
}
}
' file.txt
NF表示列数
NR表示行数
首先遍历整个文件,将字段记录到数组中,然后行列互换输出。
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.
For example, if
file.txthas the following content:
name age alice 21 ryan 30
Output the following:
name alice ryan
age 21 30
题意:行列互换
思路:没有思路,看了别人使用awk完成的,然后自己写了一遍,感觉awk好强大
最终shell脚本:
#! /bin/sh
awk '
{
for(i=1;i<=NF;i++)
{
a[NR,i]=$i
}
}
END {
for(j=1;j<=NF;j++)
{
str=a[1,j]
for(i=2;i<=NR;i++)
{
str=str" "a[i,j]
}
print str
}
}
' file.txt
NF表示列数
NR表示行数
首先遍历整个文件,将字段记录到数组中,然后行列互换输出。
相关文章推荐
- leetcode-shell-192. Word Frequency
- Linux shell获取执行脚本文件所在的目录
- leetcode-shell-193. Valid Phone Numbers
- Xshell5下利用sftp上传下载传输文件
- 用 Data 定义变量
- Shell之awk详解
- shell if判断
- shell脚本收集
- Linux Shell系列教程
- PowerShell Pester 使用 - Assertion
- Shell ${}、##和%%使用范例
- Linux下的shell脚本的字符串截取
- Shell文件重定向的3种方法
- Linux下bash配置及执行顺序
- linux - python shell auto completion
- 4bash中对日期的使用
- 3对符合条件的文件和文件夹进行复制或修改
- 【shell】变量的配置文件
- 2文件及文件夹查找
- linux中环境变量配置bashrc与profile的区别