|
|
|
Your skill will accomplish what the force of many cannot |
|
| PerlMonks |
Changing string in specific line/position in a fileby onemojofilter (Novice) |
| on Oct 31, 2022 at 17:11 UTC ( [id://11147864]=perlquestion: print w/replies, xml ) | Need Help?? |
|
onemojofilter has asked for the wisdom of the Perl Monks concerning the following question:
Good morning:
I am trying to take a spool file and replacing a string in a specific line, position within that file based on values found in other places in the file. As an example: File sample contents:
Firstly, the first line will display something like an "H" and each subsequent "H" in the file will be preceded by "^L" (which signifies a new "page") Secondly, I will take the value found on line 10 (relative to where "H" appears") on position 25 through 35 (in this case 20221103) and replace it with another value based upon the value found on line 14, positions 25 through 35. (Basically a database lookup, which I can do). Google searches seem to only mention either using 'sed' (which I can readily do in a shell script, but am trying to do this in a more efficient and readable way using Perl) or looping through the file (which I'm already doing in a shell script). Is the only way to change a specific line/position in a file really going to be looping through the entire file? I was hoping there was a method by which I could specify a line and a position and extract (and replace as needed - writing that portion back to the file - sort of how 'sed -i' would do, but something more perlish?) that instead of slurping the entire file. Any thoughts/suggestions appreciated!
Back to
Seekers of Perl Wisdom
|
|
||||||||||||||||||||||||||||||