tags:

views:

167

answers:

2

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
+4  A: 

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.

John Kugelman
Ah, that's exactly what I was after. Thanks!
humble coffee
A: 

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/"
Charles Ma
Actually I didn't need the file name appended, that was just to indicate where the line came from. Sorry, I could have been clearer.
humble coffee