<critic level="harsh">
- Your code does not produce the output desired by the OP.
- You should use warnings; because it will generate warning messages which point to bugs in the code, particularly the odd usage of sprintf.
- You should always check the success of open.
- There is no indentation, which makes it difficult to understand the code.
- use strict; to avoid other common programming mistakes.
- It is a good practice to use the 3-argument form of open and to use lexical filehandles:
open my $fh, '<', 'txt1.txt' or die "Can not open txt1.txt: $!";