Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?

0
Posted

How do I change one line in a file/delete a line in a file/insert a line in the middle of a file/append to the beginning of a file?

0

Those are operations of a text editor. Perl is not a text editor. Perl is a programming language. You have to decompose the problem into low-level calls to read, write, open, close, and seek. Although humans have an easy time thinking of a text file as being a sequence of lines that operates much like a stack of playing cards — or punch cards — computers usually see the text file as a sequence of bytes. In general, there’s no direct way for Perl to seek to a particular line of a file, insert text into a file, or remove text from a file. (There are exceptions in special circumstances. You can add or remove at the very end of the file. Another is replacing a sequence of bytes with another sequence of the same length. Another is using the $DB_RECNO array bindings as documented in the DB_File manpage. Yet another is manipulating files with all lines the same length.) The general solution is to create a temporary copy of the text file with the changes you want, then copy that over the ori

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123