In linux how to join 2 files

awk, join, linux, sed, shell

Solution

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

Problem

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,

Original source