如何为基于列的联合多个txt文件? [英] How to join multiple txt files into based on column?

查看:114
本文介绍了如何为基于列的联合多个txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有txt文件,所有这一切都是在同一个目录。每个人都有数据的2列。他们看起来是这样的:

I have txt files, all of which are in the same directory. Each one has 2 columns of data. They look like this:

标签1 DataA1结果
  LABEL2 DataA2结果
  LABEL3 DataA3

Label1 DataA1
Label2 DataA2
Label3 DataA3

我想用加入做出一个大文件是这样的。

I would like to use join to make a one large file like this.

标签1 DataA1 DataB1 DataC1结果
  LABEL2 DataA2 DataB2 DataC2结果
  LABEL3 DataA3 DataB3 DataC3

Label1 DataA1 DataB1 DataC1
Label2 DataA2 DataB2 DataC2
Label3 DataA3 DataB3 DataC3

目前,我有

加入的fileA FILEB |加入 - fileC

join fileA fileB | join - fileC

不过,我有太多的文件,使其实际列出所有的人 - 有没有编写这种命令的循环方式。

However, I have too many files to make it practical to list all of them - is there a way to write a loop for this sort of command?

推荐答案

使用bash,那么你可以创建一个脚本,做一个递归管EXEC的捧场:

With bash you could create a script that does a recursive pipe exec for join:

#!/bin/bash

if [[ $# -ge 2 ]]; then
    function __r {
        if [[ $# -gt 1 ]]; then
            exec join - "$1" | __r "${@:2}"
        else
            exec join - "$1"
        fi
    }

    __r "${@:2}" < "$1"
fi

和文件作为参数传递给脚本,如:

And pass the files as parameters to the script like:

bash script.sh file*

或分类的形式,如:

find -type f -maxdepth 1 -name 'file*' -print0 | sort -z | xargs -0 bash script.sh

这篇关于如何为基于列的联合多个txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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