I need to apply a series of substitutions on a text file, using a filter file with the same number of lines: line n
of the filter should apply to line n
of the original file.
E.g. original file:
foo
bar
foobar
Filter file:
s/oo/uu/
s/a/i/
s/b/l/
Expected result:
fuu
bir
foolar
Since sed
will apply each filter on each line, using sed -f filterfile
is particularly inefficient (the number of lines is fairly large, so N²
is quite large as well…). Furthermore, although in my particular case I can modify the filters to avoid this issue, this command will lead to wrong results on the example.
I'm currently implementing the following approach (still trying to fix an issue with tabulations…):
paste -d'@' filterA filterB infile \
|while IFS="@" read AA BB LINE;
do
echo $LINE|"s/$AA/$BB/g"
done > outfile
But I'm wondering if there was a more elegant solution, e.g. some sed
option? (Preferably with standard GNU/Linux tools.)