AWK:从许多不同的文件中提取不同的列 [英] Awk: extract different columns from many different files

查看:690
本文介绍了AWK:从许多不同的文件中提取不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件示例

 我有一个3-10文件量:  - 不同的列数
  - 同样的行数
  - 不一致的间距(有时一个空格,其他标签,有时很多空格)** **内极文件如下面的
> 0 55.4 9.556E + 09 33
> 1 1.3 5.345E + 03 1
> ........
> 33 134.4 5.345E + 04 932
>
       ........

我需要从file4将获得列(说)1从文件1,第3栏从file2中,从file3的和塔1 7列,并通过侧将它们组合成一个单一的文件,一面。

试验1:不工作


 粘贴≤(切-d [见下文] -f1文件1)≤(切-d [见下文] -f3文件2)[...]


  
  

在这里分隔符是''或空。


试验2:与2档工作,但不是很多文件


 的awk'{
     A1 = $ 1; B1 = $ 4;
     函数getline<D2 / FILE1.TXT
     打印A1,$ 1,B1,$ 4个
}D1 / FILE1.TXT> D3 / FILE1.TXT


现在更普遍的问题:


  

如何从许多不同的文件中提取不同的列?



解决方案

在你的粘贴 / 剪切的尝试,替换剪切 AWK

  $粘贴≤(AWK'{打印$ 1}'文件1)≤(AWK'{打印$ 3}'文件2)≤(AWK'{打印$ 7}'文件3) ≤(AWK'{打印$ 1}'file4将)

File Example

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
>
       ........

I need to get column (say) 1 from file1, column 3 from file2, column 7 from file3 and column 1 from file4 and combine them into a single file, side by side.

Trial 1: not working

paste <(cut -d[see below] -f1 file1) <(cut -d[see below] -f3 file2) [...]

where the delimiter was ' ' or empty.

Trial 2: working with 2 files but not with many files

awk '{
     a1=$1;b1=$4;
     getline <"D2/file1.txt";
     print a1,$1,b1,$4
}' D1/file1.txt >D3/file1.txt

Now more general question:

How can I extract different columns from many different files?

解决方案

In your paste / cut attempt, replace cut by awk:

$ paste <(awk '{print $1}' file1 ) <(awk '{print $3}' file2 ) <(awk '{print $7}' file3) <(awk '{print $1}' file4)

这篇关于AWK:从许多不同的文件中提取不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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