Search code examples
bashawkcutcat

How to directly append columns of one file to another file


Requirement: Only grep/cut/join/cat/regex/for/while.

I am a beginner at shell utilities.

I have fileA and fileB containing equal number of rows. I want to append columns of fileB into fileA.

I am trying this (cat fileA && cat fileB) > fileC. But it isn't working as required.

Expected:

fileA:

1
2
3

fileB:

1
2
3

then fileC should have:

1 1
2 2
3 3

Solution

  • With bash:

    #!/bin/bash
    
    while true; do
      read -r f1 <&3 || break
      read -r f2 <&4 || break
      echo "$f1 $f2"
    done 3<fileA 4<fileB >fileC
    

    Output to fileC:

    1 1
    2 2
    3 3
    

    See: https://unix.stackexchange.com/a/26604/74329