0

This question already has an answer here:

I made two files, foo.txt and bar.txt

foo.txt looks as such: foo1 foo2 foo3 foo4 foo5 foo6 foo7 foo8 foo9 foo10

And bar.txt: bar1 bar2 bar3 bar4 bar5 bar6 bar7 bar8 bar9 bar10

How do you then get these two text files and output them into a text file called foobar.txt, ordering them as

foo1 bar1
foo2 bar2
foo3 bar3
...

Admin stuff: I'm getting warnings about this being a duplicate question because people have suggested combining

foo1
foo2
foo3
foo4
foo5 

and

bar1
bar2
bar3
bar4
bar5

Is the same process. If you have to do this instead you can find the question addressing text on new line combination here: Link to user2120893's Question

To address editing files, with one command, of spaced items, you need to use the answer I have marked below using awk. I kept seeing questions of 'how to combine two lists' and 'how to separate spaced lists into new line lists' but never a one line command, using awk, to separate two spaced lists by awk functions then combine them via being designated through awk. A huge thank you to Sergiy Kolodyazhnyy for finally answering my question (and only using one command to do it!). I've been looking for an answer to this for a while and an awk wizard stepped in to show us the ropes. Credit to John1024 for another great answer.

marked as duplicate by RomanPerekhrest, Jeff Schaller, Archemar, Timothy Martin, jimmij Mar 21 '18 at 19:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Decently achievable with awk :

$ awk 'NR==FNR{split($0,a);}NR!=FNR{for(i=1;i<=NF;i++) print a[i],$i}' foo.txt bar.txt
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
foo6 bar6
foo7 bar7
foo8 bar8
foo9 bar9
foo10 bar10
1

Let's start with these two files:

$ cat foo.txt 
foo1 foo2 foo3 foo4 foo5 foo6 foo7 foo8 foo9 foo10
$ cat bar.txt 
bar1 bar2 bar3 bar4 bar5 bar6 bar7 bar8 bar9 bar10

To combine them together:

$ paste <(tr ' ' '\n' <foo.txt) <(tr ' ' '\n' <bar.txt)
foo1    bar1
foo2    bar2
foo3    bar3
foo4    bar4
foo5    bar5
foo6    bar6
foo7    bar7
foo8    bar8
foo9    bar9
foo10   bar10

Or:

$ awk -v RS='[[:space:]]+' 'FNR==NR{a[NR]=$0; next} {print a[FNR],$0}' foo.txt bar.txt
foo1 bar1
foo2 bar2
foo3 bar3
foo4 bar4
foo5 bar5
foo6 bar6
foo7 bar7
foo8 bar8
foo9 bar9
foo10 bar10
1

Building on Hillsie’s answer to this similar question, if you’re using bash, you can do

paste <(tr ' ' '\n' < foo.txt) <(tr ' ' '\n' < bar.txt)

using process substitution (<(…)) to translate the spaces in your files to newlines.

1

As an alternative to the transpose-and-combine approaches posted already, here's a combine-and-transpose approach:

$ cat foo.txt bar.txt | rs -T
foo1   bar1
foo2   bar2
foo3   bar3
foo4   bar4
foo5   bar5
foo6   bar6
foo7   bar7
foo8   bar8
foo9   bar9
foo10  bar10
  • @G-Man rs needs to be installed, it's not a built-in. It's available on Debian and FreeBSD. It's also not UUOC because cat does exactly what it's supposed to - join two text files together here, and redirect that via pipe to rs that interprets each line as separate array. The < input.txt way isn't the best in this particular case – Sergiy Kolodyazhnyy Mar 21 '18 at 6:09
  • Sorry, I need new glasses; for a moment I thought I saw only one filename there. – G-Man Mar 21 '18 at 6:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.