Linux 使用脚本组织文件

Linux 使用脚本组织文件,linux,bash,awk,scripting,Linux,Bash,Awk,Scripting,我有两个平面文件,格式如下: File1.txt Customer1 12345 12346 12347 Customer2 14444 14445 File2.txt 12345 aol.com 12347 gmail.com 12346 google.com 14444 yahoo.com 14445 outlook.com 我需要能够将文件翻译成如下内容: Customer1 aol.com google.com gmail.com Customer2 yahoo.com outloo

我有两个平面文件,格式如下:

File1.txt

Customer1 12345 12346 12347
Customer2 14444 14445
File2.txt

12345 aol.com
12347 gmail.com
12346 google.com
14444 yahoo.com
14445 outlook.com
我需要能够将文件翻译成如下内容:

Customer1 aol.com google.com gmail.com
Customer2 yahoo.com outlook.com 
这就是我目前所拥有的

$ awk 'NR==FNR {a[$1]=$2; next} $2 in a {print $0, a[$2]}' OFS='\t' File2.txt File1.txt

但是,这只查看第一个文件的第2列,如何将其展开以查看文件1中的所有列?您可以让awk遍历该文件,将
NF==2
NF==3
记录分离为两个哈希表/数组,然后链接两个数组并打印输出

您还可以对该文件进行两次检查,以执行相同的逻辑


我认为这为您提供了一个良好的开端。

awk
可以通过字段循环。试试这样的-

$: awk 'NR==FNR {a[$1]=$2; next;}
        { printf "%s ", $1; 
          for (i=2;i<=NF;i++) {
            printf "%s ", a[$i]; 
          };
          printf "\n";
        }' File2.txt File1.txt
Customer1 aol.com google.com gmail.com
Customer2 yahoo.com outlook.com
$:awk'NR==FNR{a[$1]=$2;下一步;}
{printf“%s”,$1;

对于(i=2;我会是什么样子?对不起,我不太熟悉编写脚本,所以不欢迎使用SO工作代码。@KeithWas能够将它分解一点,这样我现在有两个不同的文件,使awk更易于使用。我们能够得出以下“$awk’NR==FNR{a[$1]=$2;next}$2在a{print$0,a[$2]}“OFS=”\t“File2.txt File1.txt”但它只搜索文件1的第2列,您知道如何将其展开以应用于文件1中的所有列吗?是否使用三行“.”分隔数据?@A.Villegas这三行“.”只是更多数据的一种表示形式,它遵循与上述行相同的模式。
…实现这一点的最佳方法是什么?
-我会选择一种使文本处理更容易的编程语言,即awk、Ruby或Perl。