简体   繁体   中英

In linux how to join 2 files

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

注意join不支持在输出格式中指定一系列字段,因此以下有点hacky,但在“代码行”中最多支持8个空格

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
sed 's#([^ ]*) ([^ ]*) (.*)#$2 $1 $3#g'

注意:也许您必须使用退格键来逃避常规括号才能使其正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM