将文本文件转换为列 [英] Convert a text file into columns

查看:122
本文介绍了将文本文件转换为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我有科学数据,所有数字都排列在一个列中,但表示强度矩阵为 n (宽度)乘以 m (高度).输入文件的列总共有 n * m 行.一个输入示例可能看起来像这样:

Let's assume I have scientific data, all numbers arranged in a single column but representing an intensities matrix of n (width) by m (height). The column of the input file has in total n * m rows. An input example may look like that:

1  
2  
3  
......  
30 

新的输出应该是这样的:我有 n 个新列,其中有 m 行.以我的示例为例,输入30个字段,并且 n = 3, m = 10,我需要一个这样的输出文件(分隔符没什么大不了,可能是空白,标签等):

The new output should be such that I have n new columns with m rows. Sticking to my example with 30 fields input and n = 3, m = 10, I would need an output file like this (separator does not matter much, could be a blank, a tab etc.):

1   11  21  
2   12  22  
... ... ...
10  20  30 

我在Windows下使用gawk.请注意,没有特殊的FS,更多实际示例为60 * 60或更大.

I use gawk under Windows. Please note that there are no special FS, more real-world examples are like 60 * 60 or bigger.

推荐答案

如果您不仅限于awk,还拥有GNU核心实用程序(cygwin,native,..),那么最简单解决方法是使用pr:

If you are not limited to awk but have GNU core-utils (cygwin, native, ..) then the simplest solution is to use pr:

pr -ts" " --columns 3 file

这篇关于将文本文件转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆