Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to combine contents of 2 text files, word by word.

The script takes every word from file 1 and combines with file 2. it adds all the combined words in file 3.


File1        File2         
WordA1       WordB1
WordA2       WordB2
WordA3       WordB3

Output: WordA1 WordB1 WordA2 WordB2 WordA3 WordB3

How do we do this using the cat command?

share|improve this question
Can you actually write a question and then post it? An edit every 5 seconds is annoying. Esp. if you get multiple answers before you finished editing (and while the actual question seems to be mutating to a different question). – Hennes Mar 13 '13 at 11:52
I don't think cat would do that. You're probably going to need at the very least something like cut, and a pretty interesting bash script. Using a proper programming language like python is very likely a better idea. – Journeyman Geek Mar 13 '13 at 11:53
@sam: I edited the file and added an example. Please check if that is actually what you wanted. Also, do you really need to use cat? It is possible but probably not the best way to solve this. If you do not require the use of the cat command but just the described result, then please use the edit function to remove the How do we do this using the cat command? line. – Hennes Mar 13 '13 at 12:01
@Hennes, i just read his revisions, he need A1B1 A1B2 A2B3 A2B1 A2B2..., a small script with 2 loops is what he wanted. – LiuYan 刘研 Mar 13 '13 at 12:03

Try doing this :

paste file1 file2

If you want it on only one like :

paste file1 file2 | sed -n '2,$p' | paste -sd ' '
share|improve this answer
cat file1 file2 > outputfile.txt
share|improve this answer
but i need to combine the words.not just combine the files. – sam Mar 13 '13 at 11:52
that cannot be done from the cat command, at least not alone. – Frank Thomas Mar 13 '13 at 12:01
Aye. not alone. You can cat all the files num_words_in_file times and use cut -d' ' -f $increasing field. Seeems horribly wasteful and only useful if you need to use cat (e.g. for a homework question). – Hennes Mar 13 '13 at 12:02
[max@localhost ~]$ cat file1
[max@localhost ~]$ cat file2
[max@localhost ~]$ paste -s file1 file2 > file3
[max@localhost ~]$ cat file3 
wordA1  wordA2  wordA3
wordB1  wordB2  wordB3
share|improve this answer
sorry for the multi new here. – sam Mar 13 '13 at 12:33
i need A1B1 A1B2 A2B3 A2B1 A2B2.but all in a single line – sam Mar 13 '13 at 12:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .