freebie:~ 716> perldoc -q 'delete a line'
Found in /System/Library/Perl/5.8.6/pods/perlfaq5.pod
How do I change one line in a file/delete a line in a file/inse
+rt a
line in the middle of a file/append to the beginning of a file?
Use the Tie::File module, which is included in the standard dis
+tribu-
tion since Perl 5.8.0.
| [reply] [d/l] |
As mentioned (indirectly) by Fletch, Tie::File probably fits your needs best. It allows you to treat a file as an array. Anything you do to the array will be reflected by the file. I just wanted to provide some usage tips:
To remove a line from the file, use splice. For example, splice(@tied_to_file, 10, 1); removes one line, starting with the 11th one (0-based indexes).
splice is also used to insert lines. For example, splice(@tied_to_file, 10, 0, @lines_to_insert); inserts the provided lines before the 11th line (0-based indexes).
You can use grep to apply filters to every line of your file. For example, @tied_to_file = grep { !/DELETE ME/ } @tied_to_file;
You can use map to apply transformations to every line of your file. For example, @tied_to_file = map { uc($_) } @tied_to_file;
| [reply] [d/l] [select] |
I'd like to take it a step further; Is it possible to write to a specified spot in a file, (not the beginning or end...the middle).
Take a look at Tie::File and see what you think.
It allows you to treat a file as if it were an array
and do the the file anything else you would do to an
array of scalars.
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
| [reply] |
It sounds like seek or sysseek is what you are looking for. | [reply] |
I thought seek was only to find the beginning and/or end of a file?
| [reply] |
seek will allow you to position the file pointer to any offset within the file. The problem is that files are ordered sequences of octets, not lines. Unless you're dealing with fixed line lengths you're more than likely going to overwrite something if you try and do things in place yourself. You want to use the module mentioned in the FAQ I've referenced.
| [reply] [d/l] |