You can run sed in a loop, like this:
$ while read letter; do
printf '%s\n\n' "$(sed "s/.$/$letter/" beta.txt)";
done < screen.txt > tmpFile && mv tmpFile beta.txt
$ cat beta.txt
cvvbbd
etgjiud
qwrfggd
cvvbbm
etgjium
qwrfggm
cvvbba
etgjiua
qwrfgga
cvvbbo
etgjiuo
qwrfggo
If you want it as a script, you can just save this as gamma.sh:
#!/bin/sh
tmpFile=$(mktemp)
while read letter; do
printf '%s\n\n' "$(sed "s/.$/$letter/" beta.txt)";
done < screen.txt > "$tmpFile" && mv tmpFile
Note that this will be extremely slow and impractical for large files. It's fine for the dummy example you gave, but if you need to deal with a few thousand or even hundreds of lines, you should use another language. For example, in Perl:
#!/usr/bin/perl
use strict;
use warnings;
my @letters;
open(my $letterFile, '<', "$ARGV[0]") or
die("Failed to open letter file '$ARGV[0]':$!\n");
while (my $line = <$letterFile>) {
chomp($line);
$line =~ /(.)\s*$/;
push @letters, $1;
}
close($letterFile);
open(my $dataFile, '<', "$ARGV[1]") or
die("Failed to open data file '$ARGV[1]':$!\n");
my @data = <$dataFile>;
my $c = 0;
foreach my $letter (@letters) {
print "\n" if $c;
foreach my $line (@data) {
$line =~ s/.$/$letter/;
print "$line";
}
$c++;
}
close($dataFile);
If you save the script above as foo.pl, you can do:
$ perl foo.pl screen.txt beta.txt
cvvbbd
etgjiud
qwrfggd
cvvbbm
etgjium
qwrfggm
cvvbba
etgjiua
qwrfgga
cvvbbo
etgjiuo
qwrfggo
Note that this will store all of beta.txt in memory which might be an issue for huge files. If that is a problem, then you will need a different approach but that's beyond the scope of the current question.