How to join 2 files in Linux
I have 2 files file_A and file_B. File file_A contains the file name, followed by a space followed by a line of code. This line of code can contain characters of random types, such as spaces, :, and so on. It looks like this. Notice that the lines of code in the file are not surrounded by (). This is for illustrative purposes only.
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 file_B contains frequencies in file_name and file_A
bash$cat file_B
file_name1 2
file_name2 3
I want the output to be: (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 the join field as the first 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 first field?
I
know that with join I can use the -o format and mention the fields and order I want in the output. But how do I say put everything in a line of code (it can contain anything, so there are no separators).
Thanks,
Solution
join file_B file_A | awk '{t=$1; $1=$2; $2=t; print}' > file_C