-1

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
Cyrus
  • 84,225
  • 14
  • 89
  • 153
user10143594
  • 59
  • 1
  • 1
  • 8

2 Answers2

2

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

Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

One way using Perl

$ perl -e ' BEGIN { @a=map{chomp;$_} qx(cat fileA); @b=map{chomp;$_} qx(cat fileB); printf("%s %s\n",$a[$_],$b[$_]) for (0..$#a) } '
1 1
2 2
3 3

$

Generally, it is done using paste command

$ paste -d" " fileA fileB
1 1
2 2
3 3

$

Your inputs

$ cat fileA
1
2
3

$ cat fileB
1
2
3

$

paste command just joins files vertically using the default tab delimiter. You can override the delimiter using the -d option

stack0114106
  • 8,534
  • 3
  • 13
  • 38