在Linux中使用bash脚本合并两个文本文件 [英] merge two text files using bash scripts in linux

查看:687
本文介绍了在Linux中使用bash脚本合并两个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本文件.第一个看起来像这样:

I have two text files. the first one looks like this:

a  
b  
c  

另一个文件如下所示:

1 2   
3 4  

我想在Linux中使用bash脚本合并这两个文件,以便将第一个文件的每一行放置在第二个文件的所有行旁边,并且输出如下所示:

I want to use bash scripts in Linux to merge these two files so that each row of the first file will be placed next to all the rows of the second file and output looks like this:

a 1 2    
a 3 4  
b 1 2  
b 3 4  
c 1 2  
c 3 4  

任何帮助将不胜感激

推荐答案

您可以像这样使用awk:

awk 'NR==FNR{a[++n]=$0; next} {for (i in a) print $0, a[i]}' file2 file1

a 1 2
a 3 4
b 1 2
b 3 4
c 1 2
c 3 4

参考:有效的AWK编程

这篇关于在Linux中使用bash脚本合并两个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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