Faster execution time for bash script

Issue

Hi: I made a script to combine different possibilities with files, but my files has 1000 lines each one, and with awk and echo it takes soo long to generate the output file. Is there anyway to do the same faster?

Example:

fileA.txt is:
dog
cat
horse
fish

fileB.txt is:
good
bad
pretty
ugly

I need fileC to be like:
doggood
dogbad
dogpretty
dogugly
catgood
catbad
catpretty
catugly
etc

Here`s the code:

#!/bin/bash
numA=1
while [ $numA -le 1000 ]; do
numB=1
    while [ $numB -le 1000 ]; do
        string1=$(awk "NR==$numA" fileA.txt)    
        string2=$(awk "NR==$numB" fileB.txt)
        string3="$string1$string2"
        echo "$string3" >> fileC.txt
        numB=$(($numB+1))
    done
    numA=$(($numA+1))
done

it will took weeks. I am new to bash scripting, so if someone has any idea, with a code example will be fine. Thanks

Solution

If one of the files can fit in memory:

awk 'NR==FNR {a[++n]=$0; next} {for (i=1; i<=n; ++i) print $0 a[i]}' fileA fileB

With that example input,

#!/bin/sh -

awk '
  NR==FNR {
      a[++n]=$0
      next
  }

  {
      for (i=1; i<=n; ++i) {
          print $0 a[i]
      }
  }
' fileB.txt fileA.txt > fileC.txt

Answered By – rowboat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published