Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Search and Update

Status
Not open for further replies.

rhnewfie

Programmer
Jun 14, 2001
267
CA
Is it possible to search a text file for a specific line, the line number I will know from the start, and then only update that one line? It sounds so simple but I don't think that it is.

Thanks For Any Help
RHNewfie

There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
yes. which line do you want to update - and with what?

Duncan
 
I may have to update any line with the line that is already there plus some extra text. I also cannot do this from the command line. It must be inside a script.

There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
rh,

pls drop the sig...pls
4 lines that sez f**k all.

Give us some context, and code would be good

-Rgds
---Paul
 
Basically I have a text file with the following content

1:22
2:45
3:48
4:1

I need to search that file for one of those lines and increment the number associated with the label.

This is what I am doing so far
To start $keyword = "2:";
if (-f $file)
{
open(OUTPUT, ">>$file");
while($inline=<OUTPUT>)
{
chop($inline);
if($inline =~ /$keyword/i)
{
$count = substr($inline, 3);
$count = $count + 1;
$outline.= $keyword.&quot;:&quot;.$count;
print OUTPUT $outline;
print OUTPUT &quot;\n&quot;;
close (OUTPUT);
exit;
}
}

But I don't think that it will work this way.
 
can you give an example?

this is your text file...

1:22
2:45
3:48
4:1

what might you search want to search for & increment?

Duncan
 
I would want to search for line 2

2:45

and then output

2:46

Thanks
 
are these clock times?

$time = &quot;2:45&quot;;

($hour, $minutes) = split (/:/, $time);

$minutes++;

print &quot;$hour : $minutes\n\n&quot;;


Regards
Duncan
 
No. The #: is a line label and the number is the amount of times that a file is accessed.

Thanks
RHN
 
the example will still work

just split up the line on the colon, assign scalars to the values and increment the right hand side

$data = &quot;2:45&quot;;

($label, $accessed) = split (/:/, $data);

$accessed++;

print &quot;$label : $accessed\n\n&quot;;


Regards
Duncan

Duncan
 
rh,
my apologies, impulse typing, I had a meeting with some marketing types, and they wrecked my head.

I have no right to censure any sig.

Again sincere apologies

--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top