Home > Back-end >  How to read 2 files and write content to another file using a shell script
How to read 2 files and write content to another file using a shell script

Time:11-05

I have 2 files which look like this.

file-A

Red 
Green 
Blue
Yellow

file-B

Car
Bus 
Van
Bike

I have to write content of them to file-C by following the defined variable. (Every time file-A and file-B line count will be equal)

expected output:

Red Car
Green Bus Green
Blue Blue Blue
Yellow

This is what I tried (I must follow this way)

mycolor="file-A"
myvehicle=$(cat file-B)

while read -r color
do

  for vehicle in $myvehicle
  do
  
    echo $color $vehicle
    echo $color $vehicle $color
    echo $color $color $color
    echo $color 

    
  done 
     
done <$mycolor > file-C

then output I got

Red Car
Red Car Red
Red Red Red
Red
Red Bus
Red Bus Red
Red Red Red
Red
Red Van
Red Van Red
Red Red Red
Red
Red Bike
Red Bike Red
Red Red Red
Red
Green Car
Green Car Green
Green Green Green
Green
Green Bus
Green Bus Green
Green Green Green
Green
Green Van
Green Van Green
Green Green Green
Green
Green Bike
Green Bike Green
Green Green Green
Green
Blue Car
Blue Car Blue
Blue Blue Blue
Blue
Blue Bus
Blue Bus Blue
Blue Blue Blue
Blue
Blue Van
Blue Van Blue
Blue Blue Blue
Blue
Blue Bike
Blue Bike Blue
Blue Blue Blue
Blue
Yellow Car
Yellow Car Yellow
Yellow Yellow Yellow
Yellow
Yellow Bus
Yellow Bus Yellow
Yellow Yellow Yellow
Yellow
Yellow Van
Yellow Van Yellow
Yellow Yellow Yellow
Yellow
Yellow Bike
Yellow Bike Yellow
Yellow Yellow Yellow
Yellow

Can someone help me to figure out this? Thanks in advance!

Note: I am not allowed to use jq or other languages as JavaScript, Python etc.

CodePudding user response:

I would harness GNU AWK for this task following way, let file1.txt content be

Red 
Green 
Blue
Yellow

and file2.txt content be

Car
Bus 
Van
Bike

then

awk 'FNR==NR{arr[FNR]=$1;next}FNR==1{print arr[1],$1}FNR==2{print arr[2],$1,arr[2]}FNR==3{print arr[3],arr[3],arr[3]}FNR==4{print arr[4]}' file1.txt file2.txt

gives output

Red Car
Green Bus Green
Blue Blue Blue
Yellow

Explanation: FNR is number of row inside processed file, NR is number of row globally, therefore action with pattern FNR==NR is applied solely to 1st file rammed. When processing first file I simply fill array arr so that keys are number of lines and values are 1st fields and as I do not want anything else I instruct GNU AWK to go to next row. When processing further files I print depending on number of row, colour is retrieved from array arr using key being same as number of row.

(tested in GNU Awk 4.0.0)

CodePudding user response:

$ paste -d' ' file-A file-B
Red Car
Green Bus
Blue Van
Yellow Bike

But if you insist in using bash variables:

$ paste file-A file-B | while read -r a b; do printf '%s %s\n' "$a" "$b"; done
Red Car
Green Bus
Blue Van
Yellow Bike

Or:

$ mapfile -t a < file-A
$ mapfile -t b < file-B
$ for (( i=0; i<${#a[@]}; i   )); do printf '%s %s\n' "${a[i]}" "${b[i]}"; done
Red Car
Green Bus
Blue Van
Yellow Bike
  • Related