bash - 如何将一个文件逐行放入另一个文件中

标签 bash file text-processing

我有两个行号完全相同的文件。它们如下所示:

File-A
X1 Y1 Z1 T1
X2 Y2 Z2 T2

File-B
M1 N1
M2 N2

所以,我想合并它们,以便最终文件(File-C)如下所示:

X1 Y1 M1 Z1 T1
X2 Y2 M2 Z2 T2

简单地说,我需要获取 File-B 中每行的第一个字段并将其作为第三个字段放入最终文件中。我怎样才能用 bash 命令做到这一点?

编辑

在提供答案后,我尝试合并这些文件,但根据该列上的值更改来自文件 B 的列。因此,通过以下方式,我想要 file-C 如下:

  X1 Y1 "foo" Z1 T1    #if M1 == 3
    X2 Y2 "boo" Z2 T2  # if M2 == 4 
    X3 Y3 "goo" Z3 T3  # if M2 == 2
    X4 Y4 "too" Z4 T4  # if M2 == 1

保证 M2 将是这些值之一。

最佳答案

您可以尝试关注 awk 并让我知道这是否对您有帮助。

awk 'FNR==NR{a[FNR]=$1 FS $2;b[FNR]=$3 FS $4;next} {print a[FNR],$1,b[FNR]}' File-A File-B

输出如下。

X1 Y1 M1 Z1 T1
X2 Y2 M2 Z2 T2

如果您想将此输出放入名为 C 的输入文件中,只需在上述命令末尾添加 > "File-C" 即可。

现在也添加非单线性形式的解决方案并附有代码解释。

awk '
FNR==NR{                ##FNR==NR condition will be TRUE when first Input_file named File-A is being read. If this condition is TRUE do following.
  a[FNR]=$1 FS $2;      ##Creating an array named a whose index is current line and value is 1st and 2nd columns with a space in between them.
  b[FNR]=$3 FS $4;      ##Creating an array named b whose index is current line number and value is 3rd and 4th column with a space in them.
  next                  ##using next will make sure NO further statements are being used.
}
{
  print a[FNR],$1,b[FNR]##This print statement will be executed when 2nd Input_file named File-B is being read because during first Input_file read it will NEVER come on this statement, so simply printing here array a value with index of current line number then printing 1st column and then printing array b whose index is current line.
}
' File-A File-B         ##Mentioning Input_files here.

编辑1:由于OP在实际问题本身中添加了一个小要求,因此也在此处添加编辑后的解决方案。

awk 'FNR==NR{a[FNR]=$1 FS $2;b[FNR]=$3 FS $4;next} $1==3{val="foo"} $1==4{val="boo"}$1==2{val="goo"} $1==1{val="too"}{print a[FNR],val,b[FNR]}' File-A File-B

关于bash - 如何将一个文件逐行放入另一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492778/

相关文章:

bash - 如何引用命令替换以使其表现得像 "$@"?

linux - cURL cookie 语法(来自 bash CLI,不是 cookie 文件)

Android应用程序无法读取外部存储文件

c - 如何删除C程序中的文件?

linux - 生成进程并将当前 bash 脚本的输出重定向到它

bash - 如何撤消命令 $ eval "$(docker-machine env blog)"

python - sklearn countvectorizer中的fit_transform和transform有什么区别?

java - OpenCV Android(java)字符检测和字体识别

Java - 使用 BufferedWriter 和 BufferedReader,

regex - 使用 Perl 提取内容