linux - 在linux中如何加入2个文件

标签 linux shell join sed awk

我有 2 个文件 file_A 和 file_B。文件 file_A 包含文件名,然后在空格后是代码行。 此代码行可以包含随机类型的字符,如空格、: 等。它看起来像这样。请注意,文件中的代码行没有被 () 包围。这仅用于说明目的。

bash$ cat file_A

file_name1 (code line a)
file_name1 (code line b)
file_name2 (code line c)
file_name2 (code line d)
file_name2 (code line e)

文件 file_B 包含 file_name 以及 file_A 中的频率

bash$cat file_B

file_name1 2
file_name2 3

我希望输出为:(frequency,file_name,code_line)

2 file_name1 (code line a)
2 file_name1 (code line b)
3 file_name2 (code line c)
3 file_name2 (code line d)
3 file_name2 (code line e)

bash$ join -1 1 -2 1 file_B file_A > file_C

我得到 file_C 作为(我得到连接字段作为第一个字段)

file_name1 2 (code line a)
file_name1 2 (code line b)
file_name2 3 (code line c)
file_name2 3 (code line d)
file_name2 3 (code line e)

如何获取第一个字段中的频率字段?

我知道通过 join 我可以使用 -o 格式并在输出中提及我想要的字段和顺序。但是我怎么说把所有放在代码行中(它可以包含任何东西,所以没有分隔符)

谢谢,

最佳答案

join file_B file_A | awk '{t=$1; $1=$2; $2=t; print}' > file_C

关于linux - 在linux中如何加入2个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4379338/

相关文章:

java - 无法获取配置文件 : export: Command not found

string - 将两个字符串连接在一起而不在 Ruby on Rails 中添加?

c++ - 接受错误 : Bad file descriptor

linux - psplash 图像未出现(yocto 和 qemu)

linux - centos 7 AWS(使用 ssh key 和密码登录)

shell - UNIX shell 脚本中的 "#!/bin/sh"行是什么意思?

linux - 唤醒时执行 bash 脚本

shell - 如何在shell中的一行中编写if else?

mysql - 我的查询有什么错误?我正在尝试从多个表中选择数据

mysql - 选择在同一个月内从两个特定类别中至少购买了一个的客户数