Re: Script to update a value in text file
by CountZero (Bishop) on Jun 15, 2010 at 05:53 UTC
|
What have you tried yourself?
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] |
Re: Script to update a value in text file
by Your Mother (Archbishop) on Jun 15, 2010 at 06:03 UTC
|
perl -pe 's/\A([a-z]+)/$i=$1;++$i/e' [filename]
And sno & cno will be incremented. Um… CountZero has a more useful, if less literal, reply, really.
| [reply] [d/l] |
Re: Script to update a value in text file
by biohisham (Priest) on Jun 15, 2010 at 06:03 UTC
|
Welcome to the Monastery, Advising you to try to make your own script that does this job and then once you've fallen short we can pick it up from there as this site is not a code-writing service provider.
Show us what you have tried so far and we'd pick it up from there, but for starters, think about a hash, it is not clear if you want an incrementing that is uniform or random since you've not mentioned that.
Think about reading sno:10 and cno:10 into a hash and increment the hash values...
Also relevant, read How do I post a question effectively?, Markup in the Monastery and Writeup Formatting Tips
Excellence is an Endeavor of Persistence.
A Year-Old Monk :D .
| [reply] [d/l] [select] |
Re: Script to update a value in text file
by kejohm (Hermit) on Jun 15, 2010 at 06:33 UTC
|
| [reply] |
Re: Script to update a value in text file
by Boldra (Deacon) on Jun 15, 2010 at 12:03 UTC
|
in-place-edit is appropriate (YourMother's reply), but just because I'm still amazed at how cool IO::All is:
perl -MIO::All -E 's/(\d+)/$1+1/e for @{io("/tmp/amvarma.txt")}'
| [reply] [d/l] |
Re: Script to update a value in text file
by Marshall (Canon) on Jun 15, 2010 at 06:17 UTC
|
The file on disk is a sequence of bytes. You can't simply insert or
delete bytes because all the bytes after that have to be moved.
For a small file, read the file into memory, make modifications as
you will and then re-write the file. You may want to make some sort
of backup scheme so that if your program dies while re-writing the
file, the orginal data can still be recovered (i.e. don't delete,
file and re-write with the same name because if that write fails,
all data is lost). | [reply] |
Re: Script to update a value in text file
by JavaFan (Canon) on Jun 15, 2010 at 10:23 UTC
|
Can I get some help, how to go about in implementing this?
First, I'd learn a language of my choice. Then, in said language, I would:
- Get coffee (I recommend Java).
- Open the original file for reading.
- Open a new file for writing.
- Read in the original file, in chunks.
- For each chunk:
- Locate data I want to modify.
- Change the chunk so the value is incremented.
- Write the modified chunk to the new file.
- Close both open files.
- Move the new file over the original one.
- Finish coffee.
- Profit!
| [reply] |
|
6a. Rename original file (so it's still around for post mortems)
| [reply] |
Re: Script to update a value in text file
by Khen1950fx (Canon) on Jun 16, 2010 at 01:09 UTC
|
This increments sno and cno, but I did this just for fun.
#!/usr/bin/perl
use strict;
use warnings;
use Tie::CountLoop;
tie my $counter, 'Tie::CountLoop', 'sno:10';
tie my $counter1, 'Tie::CountLoop', 'cno:20';
my $t = tied $counter;
my $t1 = tied $counter1;
$t->auto(1);
$t1->auto(1);
print " <<$counter>>:10 \n",
" <<$counter1>>:20 \n";
| [reply] [d/l] |