file1.csv
A,,C,D
A,,C,D
A,,C,D
A,,C,D
file2.csv
A,B
A,B
A,B
A,B
desired Output.csv
A,B,C,D
A,B,C,D
A,B,C,D
A,B,C,D
I've tried using "join" and "paste" to no avail. Is there a bash command to do this? Column "A" is the same in both .csv
files.
Best Answer
With only
awk
command:Get a line from file1 and store it into local variable
f1
, then print the line that stored inf1
and finally print the third($3
) and forth($3
) fields from file1 which delimited with comma,
altogether, and change the OFS(output field separator [space by default]) to comma(,
).The short command would be like this:
paste the file2, then cut and paste the third column to the next(
-f3-
) from file1.With
awk
andpaste
(option A)Below command also copies the last two columns (
C,D
) from file1 at the end of each line in file2:Above command paste the file2 content then print a comma delimiter(
-d','
) then paste the two last field(NF
is the index of last field and$NF
is the string which its index isNF
. So$(NF-1)
is the second field before last field ) from file1 when those index redefines or splits with comma spectator(-F','
).With
awk
andpaste
(option B)This command also is the same as above(
$3
and$4
points to third and forth field of each line from file1 ):Or another solution with
cut
command:cut command in above command first cut the first field(
-f1
which indexed with comma delimiter(-d.
)) from file1(cut -d, -f1 file1
), then cut and paste the second field of file2(cut -d, -f2 file2
) and finally cut and paste the third column(-f3
) to the nexts(-
) from file1(cut -d, -f3- file1
) again.This command also returns the same result:
paste the second field from file1(
awk -F',' '{print $1}' file1
) then print a comma(-d,
), then paste the second column from file2(awk -F',' '{print $2}' file2
), finally paste the second and last column of file1(awk -F',' '{print $3","$4}' file1
) again.