Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine columns from different files

Tags:

bash

sed

awk

perl

I have two text files that have these structures:

File 1

Column1:Column2
Column1:Column2
...

File 2

Column3
Column3
...

I would like to create a file that has this file structure:

Column1:Column3
Column1:Column3
...

Open to any suggestions, but it would be nice if the solution can be done from a Bash shell, or sed / awk / perl / etc...

like image 685
bag-man Avatar asked Dec 06 '11 00:12

bag-man


2 Answers

cut -d: -f1 "File 1" | paste -d: - "File 2"

This cuts field 1 from File 1 (delimited by a colon) and pastes it with the only column in File 2, separating the output fields with a colon.

like image 70
Jonathan Leffler Avatar answered Oct 31 '22 16:10

Jonathan Leffler


Here's an awk solution. It assumes file1 and file2 have an equal number of lines.

awk -F : '{ printf "%s:",$1; getline < "file2"; print }' < file1
like image 22
sjs Avatar answered Oct 31 '22 16:10

sjs