In linux how to join 2 files

2020-03-26 06:51发布

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,

3条回答
放荡不羁爱自由
2楼-- · 2020-03-26 06:57
join file_B file_A | awk '{t=$1; $1=$2; $2=t; print}' > file_C
查看更多
做个烂人
3楼-- · 2020-03-26 07:00
sed 's#([^ ]*) ([^ ]*) (.*)#$2 $1 $3#g'

Note: Maybe you will have to escape the normal brackets with backspaces to make it work.

查看更多
Rolldiameter
4楼-- · 2020-03-26 07:04

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"

join -o 1.2,0,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9 file_B file_A
查看更多
登录 后发表回答