I have a file that looks like this:i36aasf5i7538i123i47982i16537i1256i1647i6458i3457i1856i8456i43865I want to make a copy of the file, in which the first i in every line is replaced by an o. I then want to concatenate the edited file back to the original file (preferrably without needing to specify an output file).So the output would look like this:i36aasf5i7538i123i47982i16537i1256i1647i6458i3457i1856i8456i43865o36aasf5i7538i123o47982i16537i1256o1647i6458i3457o1856i8456i43865I know some one-liners that can do this. However, when using sed, there were problems with the encoding (the file contains some unusual characters). Using perl, I didn't have that issue, but was looking for a way to fit this into a perl script as elegantly as possible.I'm using a Unix operating system.
Duplicate file, replace character, then concatenate
text processing;files
When you append data to the file you're reading, you're running the risk of entering an infinite loop and growing the file forever as you end-up processing the data you've written earlier.You could guard against that with things like:perl -pe '  BEGIN{seek(STDOUT,0,2);$end = tell STDOUT}  last if tell(ARGV) > $end;  s/i/o/' < file >> fileInside a perl script:open OUT, >>, file or die open file: $!;open IN, <, file or die open file: $!;seek(OUT,0,2) or die seek: $!;$end = tell OUT;while (tell IN < $end && <IN>) {  s/i/o/;  print OUT $_;}close IN;close OUT;