bash 如何在Bash中逐行合并两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3806874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:41:50  来源:igfitidea点击:

How to merge two files line by line in Bash

bashunix

提问by semteu

I have two text files, each of them contains an information by line such like that

我有两个文本文件,每个文件都包含一行信息,例如

file1.txt            file2.txt
----------           ---------
linef11              linef21
linef12              linef22
linef13              linef23
 .                    .
 .                    .
 .                    .

I would like to merge theses files lines by lines using a bash script in order to obtain:

我想使用 bash 脚本逐行合并这些文件以获得:

fileresult.txt
--------------
linef11     linef21
linef12     linef22
linef13     linef23
 .           .
 .           .
 .           .

How can this be done in Bash?

如何在 Bash 中做到这一点?

回答by Mark Byers

You can use paste:

您可以使用paste

paste file1.txt file2.txt > fileresults.txt

回答by ghostdog74

here's non-paste methods

这是非粘贴方法

awk

awk

awk 'BEGIN {OFS=" "}{
  getline line < "file2"
  print 
exec 6<"file2"
while read -r line
do
    read -r f2line <&6
    echo "${line}${f2line}"
done <"file1"
exec 6<&-
,line } ' file1

Bash

重击

pr -tmJ a.txt b.txt > c.txt

回答by vtha

Try following.

尝试跟随。

man paste

回答by Christopher Creutzig

Check

查看

##代码##

possible followed by some command like untabifyor tabs2spaces

可能后跟一些命令,如untabifytabs2spaces