Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zip columns from separate files together in bash

Tags:

bash

I have two files which both contain a list of words. Is there an easy way to zip the contents of the files into one new file in bash, so that the resultant file would have two columns like this:

     file1_line1 file2_line1     file1_line2 file2_line2     file1_line3 file2_line3     file1_line4 file2_line4 
like image 848
nedned Avatar asked Jun 24 '09 01:06

nedned


2 Answers

NAME

paste -- merge corresponding or subsequent lines of files

SYNOPSIS

paste [-s] [-d list] file ...

DESCRIPTION

The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character, and writes the resulting lines to standard output.

like image 115
John Kugelman Avatar answered Sep 22 '22 13:09

John Kugelman


Paste will get you half way there, but you'll need sed to append the file name to the words

Put this into a shell script and pass it the two files as arguments

#!/bin/sh paste $1 $2 | sed -e "s/^\([^ ]\+\)\s\+\([^ ]\)/$1_\1 $2_\2/" 
like image 44
Charles Ma Avatar answered Sep 21 '22 13:09

Charles Ma