在linux中如何加入2个文件
我有 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
我想要输出为:(频率,文件名,代码行)
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 格式并提及我想要输出中的字段和顺序。但是我该怎么说将所有内容放入代码行(可以包含任何内容,因此没有分隔符)这样
谢谢,
I have 2 files file_A and file_B. The file file_A contains file name and then after space the code line. This code line can have random kind of characters say blanks,: etc. It looks like this. Please note that the code line in the file is not surrounded by (). This was only for illustration purpose.
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)
The file file_B contains the file_name along with frequency in file_A
bash$cat file_B
file_name1 2
file_name2 3
I want output as: (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
I get file_C as (I get join fields as 1st field)
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)
How do I get the frequency field in the 1st field?.
I know that with join I can use -o format and mention what fields and in what order I want in the output. But how do I say that put all in the code line (which can contain anything, so no delimiter as such) as such
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意 join 不支持在输出格式中指定一系列字段,因此以下内容有点 hacky,但确实支持“代码行”中最多 8 个空格
Note join doesn't support specifying a range of fields in the output format, so the following is a bit hacky, but does support up to 8 spaces in the "code line"
注意:也许您必须使用退格键转义普通括号才能使其正常工作。
Note: Maybe you will have to escape the normal brackets with backspaces to make it work.