If we can assume that there are always 2 lines to be added (never 3 or 1 or whatever) and that the numbers are always in the second column, separated by a space, then there's an easy solution:
cut -f2 -d' ' input.txt | perl -Mbigint -nle 'print $_ + <>' > output.txt
The cut
command just picks the second column of data, and throws the first away. The perl
command loops over the incoming lines (using the -n
switch) and prints the sum of the current line and the next line (so it works in groups of two). Note the use of the bigint
module to treat the long strings as very large numbers. Finally, the output is redirected to output.txt
.
If you need the lines numbered in the output, you might consider adding cat -n
as a last step in the pipeline, or add it right in the Perl code:
cut -f2 -d' ' input.txt | perl -Mbigint -nle 'print ++$x . " " . ($_ + <>)' > output.txt
Or if you can't assume the input is formatted with spaces as in your example, you can move that processing to the Perl too:
perl -Mbigint -nle 's/.* //; $x=<>; $x =~ s/.* //; print $_ + $x' input.txt > output.txt