在linux下如何加入2档 [英] In linux how to join 2 files

查看:137
本文介绍了在linux下如何加入2档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件FILE_A和FILE_B。该文件包含FILE_A文件名,然后空间后,code线。 这code线可以有任意一种字符空格说,:等它看起来像这样。请注意,该文件中的code线没有被包围()。这只是为了说明目的。

的bash $猫FILE_A

  file_name1(code线)
file_name1(code B线)
file_name2(code线c)
file_name2(code线d)
file_name2(code线五)

该文件包含FILE_B在file_name中FILE_A频率沿

的bash $猫FILE_B

  file_name1 2
file_name2 3

我想输出:(频率,FILE_NAME,code_line)

  2 file_name1(code线)
2 file_name1(code B线)
3 file_name2(code线c)
3 file_name2(code线d)
3 file_name2(code线五)

的bash $加入-1 1-2 1 FILE_B FILE_A> FILE_C

我得到FILE_C因为(我得到的连接字段为第1场)

  file_name1 2(code线)
file_name1 2(code B线)
file_name2 3(code线c)
file_name2 3(code线d)
file_name2 3(code线五)

我如何在第一场变频领域?

我知道,与我一起可以使用-o格式,谈不上什么领域和我想在输出顺序。但我怎么说,把所有在code线(可包含任何东西,所以没有分隔符本身)这样

谢谢,


解决方案

 加入FILE_B FILE_A | AWK'{T = $ 1; $ 1 = $ 2; $ 2 =吨;打印}'> FILE_C

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,

解决方案

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

这篇关于在linux下如何加入2档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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