【发布时间】:2012-09-26 14:02:41
【问题描述】:
文件示例
I have a 3-10 amount of files with:
- different number of columns
- same number of rows
- inconsistent spacing (sometimes one space, other tabs, sometimes many spaces) **within** the very files like the below
> 0 55.4 9.556E+09 33
> 1 1.3 5.345E+03 1
> ........
> 33 134.4 5.345E+04 932
>
........
我需要从 file1 中获取第 1 列,从 file2 中获取第 3 列,从 file3 中获取第 7 列,从 file4 中获取第 1 列,并将它们并排合并到一个文件中。
试用 1:不工作
paste <(cut -d[see below] -f1 file1) <(cut -d[see below] -f3 file2) [...]分隔符为“”或为空的位置。
试用 2:使用 2 个文件,但不能使用很多文件
awk '{ a1=$1;b1=$4; getline <"D2/file1.txt"; print a1,$1,b1,$4 }' D1/file1.txt >D3/file1.txt
现在更一般的问题:
如何从许多不同的文件中提取不同的列?
【问题讨论】:
-
如何使用
cut和paste不起作用? -
我认为这是因为 cut 假设间距是恒定的。我将数据格式化为不同的间距,以便使每列左侧的数字对齐。如果一个数字有更多的数字,那么它左边的空格就会更少。