CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Urgent ( search a file for a line )

 

First page Previous page 1 2 Next page Last page  View All


sh.ajay12
User

Nov 5, 2008, 9:04 PM

Post #1 of 26 (4442 views)
Urgent ( search a file for a line ) Can't Post

Hi,

I want to search a file f1 for a line l1 and then search a another file f2 for another line l2 and replace l2 with l1.

Please let me know the script. Its very urgent for me.

Thanks,
Ajay


KevinR
Veteran


Nov 5, 2008, 11:40 PM

Post #2 of 26 (4432 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

There is no urgent help here.
-------------------------------------------------


sh.ajay12
User

Nov 5, 2008, 11:47 PM

Post #3 of 26 (4430 views)
Re: [KevinR] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi Kevin,

Thank you. i understand what you are saying but if you can provide me the code free i would be very great full to you. Because its really urgent for me.

Waiting for your reply.

Thanks,
Ajay


KevinR
Veteran


Nov 5, 2008, 11:53 PM

Post #4 of 26 (4429 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Sorry. No.
-------------------------------------------------


sh.ajay12
User

Nov 6, 2008, 12:04 AM

Post #5 of 26 (4428 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi,

If any one in the forum can help with this problem, i will be very greatful to him.

Thanks,
Ajay


KevinR
Veteran


Nov 6, 2008, 7:41 PM

Post #6 of 26 (4416 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Show some effort.... maybe get some help.
-------------------------------------------------


sh.ajay12
User

Nov 6, 2008, 11:31 PM

Post #7 of 26 (4414 views)
Re: [KevinR] Urgent ( search a file for a line ) [In reply to] Can't Post

i have a file in this format
k1 = ("C:\ADS2009\bin\ads.exe")
k2 = ("Getting Started with ADS")
k3 = ("Getting Started with ADS")
k4 = ("!f")
k5 = ("Delete Project..")

i want to open this file in read mode and read each line. split each line into k1 and the value and store in a hash.

Then i want to read another file which is in this format

Runk1
sleep(1000)
WinWaitActivek2
WinClosek3
Sendk4
Sendk5

Here i have to replace k1 with corresponding value and similarly k2 with corresponding value and similarly others.

i have written a code but the code is not working.

Please if anyone can help

Thanks
Ajay


sh.ajay12
User

Nov 7, 2008, 1:26 AM

Post #8 of 26 (4412 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

i have prepared this script to store the key and value information in a hash

#!/usr/local/bin/perl
open KEY_INFO, ("< key_value.txt");
while ( $line = <KEY_INFO> )
{
( $ke, $val ) = split ("=", $line);
$myhash{ $ke } = $val;
}
foreach $ke (keys %myhash)
{
print "\n $myhash{$ke}\n";
}

close KEY_INFO;
exit;

but how will i compare this hash value with the values in the second file which is file2.txt and which contains k1, k2, k3 and how will i replace the k1, k2, k3 ... with their values.

file2.txt

Runk1
sleep(1000)
WinWaitActivek2
WinClosek3
Sendk4
Sendk5
sleep(2000)



If any one can help please provide the code.

Thanks,
Ajay


KevinR
Veteran


Nov 7, 2008, 10:24 PM

Post #9 of 26 (4397 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Good to see some effort on your part. In the second file you need to get the 'kn' value from the end of each line to match it with a key from the first file.

Here is a general ideas of how to get the second file into a form that can be compared to the hash:


Code
my $otherhash; 
open FILE2, "< file2.txt";
while (my $line = <FILE2>) {
if ($line =~ /(k\d+)$/) {
$otherhash{$ke} = $myhash{$ke};
}
}
close FILE2;


This of course is not the solution to putting the values from file1 into the corresponding lines of file2 but if you understand the above code you can possibly figure out how to actually apply the changes to the second file. Give it a try.
-------------------------------------------------


sh.ajay12
User

Nov 8, 2008, 6:39 AM

Post #10 of 26 (4385 views)
Re: [KevinR] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi Kevin,

Thank you for the code. i have understood the code. But still i am confused why i should create another hash that would contain all keys and values of my previous hash and how will it help in replacing the values in the second file.

Thanks,
Ajay


FishMonger
Veteran / Moderator

Nov 8, 2008, 7:01 AM

Post #11 of 26 (4384 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

What is the correlation between the files? I don't see any string comparison, so is it based solely on line numbers and do they have the same number of lines?

What is the final output you expect?


sh.ajay12
User

Nov 8, 2008, 7:51 AM

Post #12 of 26 (4381 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi Kevin,

i have a template file called key_value

k1=("C:\ADS2009\bin\ads.exe")
k2=("Getting Started with ADS")
k3=("Getting Started with ADS")
k4=("!f")
k5=("Delete Project..")
k6=("SendKeyDelay", 200)
k7=("C:\users\default\sumit1_prj")
k8=("Delete Project")
k9=("Delete Project")
k10=("Delete Project")
k11=("!f")
k12=("SendKeyDelay", 200)
k13=("New Project")

and a file called auto_it

Runk1
sleep(1000)
WinWaitActivek2
WinClosek3
Sendk4
Sendk5
sleep(2000)
AutoItSetOptionk6
Sendk7
sleep(1000)
Send("{Enter}")
sleep(1000)
Send("{Enter}")
If WinActivatek8 Then
sleep(1000)
WinClosek9
sleep(1000)
WinClosek10
EndIf
Sleep(2000)
Sendk11
Send("{Enter}")
Sleep(2000)
AutoItSetOptionk12
WinWaitActivek13

I have to replace all the kn values in the above auto_it file. I have to fetch the kn values from template file key_value.

Because the kn values in the template file keeps on changing so i am making this script, so that it can be automated through the perl script.

The logic i am trying to apply.

1. The perl script should read the template file and store the kn values in the form of keys and values in a hash.
2. The perl script would then search the auto_it file and where ever it finds the keys(kn), it would replace the kn with the corresponding value from the hash.


Thanks,
Ajay


sh.ajay12
User

Nov 8, 2008, 7:54 AM

Post #13 of 26 (4379 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi Kevin,

I forgot to add few things.

The lines in the two files are not same.


Thanks,
Ajay


FishMonger
Veteran / Moderator

Nov 8, 2008, 8:11 AM

Post #14 of 26 (4377 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Your description is still not very clear, but is this what you expect as the results?

Runk1
sleep(1000)
("Getting Started with ADS")
("Getting Started with ADS")
("!f")
("Delete Project..")
sleep(2000)

if not, then please provide more details and an example of what the final results should look like.


sh.ajay12
User

Nov 8, 2008, 8:33 AM

Post #15 of 26 (4373 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi Kevin,

Here is my expected output.

the auto_it file

Runk1
sleep(1000)
WinWaitActivek2
WinClosek3
Sendk4
Sendk5
sleep(2000)
AutoItSetOptionk6
Sendk7
sleep(1000)
Send("{Enter}")
sleep(1000)
Send("{Enter}")
If WinActivatek8 Then
sleep(1000)
WinClosek9
sleep(1000)
WinClosek10
EndIf
Sleep(2000)
Sendk11
Send("{Enter}")
Sleep(2000)
AutoItSetOptionk12
WinWaitActivek13

The above script contains k1, k2, k3,...,k13

All the k1,k2,k3,..,k13 would be replaced by
k1=("C:\ADS2009\bin\ads.exe")
k2=("Getting Started with ADS")
k3=("Getting Started with ADS")
k4=("!f")
k5=("Delete Project..")
k6=("SendKeyDelay", 200)
k7=("C:\users\default\sumit1_prj")
k8=("Delete Project")
k9=("Delete Project")
k10=("Delete Project")
k11=("!f")
k12=("SendKeyDelay", 200)
k13=("New Project")


Run("C:\ADS2008U1\bin\ads.exe")
sleep(1000)
WinWaitActive("Getting Started with ADS")
WinClose("Getting Started with ADS")
Send("!f")
Send("Delete Project..")
sleep(2000)
AutoItSetOption("SendKeyDelay", 200)
Send("C:\users\default\sumit1_prj")
sleep(1000)
Send("{Enter}")
sleep(1000)
Send("{Enter}")
If WinActivate("Delete Project") Then
sleep(1000)
WinClose("Delete Project")
sleep(1000)
WinClose("Delete Project")
EndIf
Sleep(2000)
Send("!f")
Send("{Enter}")
Sleep(2000)
AutoItSetOption("SendKeyDelay", 200)
WinWaitActive("New Project")


i want to fetch k1,k2,k3,...,k13 values form template file key_value which is

k1=("C:\ADS2009\bin\ads.exe")
k2=("Getting Started with ADS")
k3=("Getting Started with ADS")
k4=("!f")
k5=("Delete Project..")
k6=("SendKeyDelay", 200)
k7=("C:\users\default\sumit1_prj")
k8=("Delete Project")
k9=("Delete Project")
k10=("Delete Project")
k11=("!f")
k12=("SendKeyDelay", 200)
k13=("New Project")

Thank you Kevin for all your help.

Thanks,
Ajay


sh.ajay12
User

Nov 8, 2008, 8:43 AM

Post #16 of 26 (4367 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post

Kevin, if you need any details. Please let me know. I would be very happy to provide you.


FishMonger
Veteran / Moderator

Nov 8, 2008, 8:44 AM

Post #17 of 26 (4366 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Please take note on who's responding to you. It's fishMonger, not Kevin.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Tie::File;

open my $template, '<', 'key_value' or die "Can't open 'key_value' $!";
tie my @auto_it, 'Tie::File', 'auto_it' or die "Can't open 'auto_it' $!";

my %template;
while ( <$template> ) {
chomp;
my ($key, $value) = split /=/;
$template{$key} = $value;
}

(tied @auto_it)->defer;
for ( @auto_it ) {
$_ = "$1$template{$2}" if /^(.+)(k\d+)$/ and exists $template{$2};
}
untie @auto_it;



(This post was edited by FishMonger on Nov 8, 2008, 8:45 AM)


sh.ajay12
User

Nov 8, 2008, 9:43 AM

Post #18 of 26 (4358 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post

Hi FishMonger,

sorry for the mistake. and Thank you for the code.

But FishMonger i did not understand this part of the code.

(tied @auto_it)->defer;
for ( @auto_it ) {
$_ = "$1$template{$2}" if /^(.+)(k\d+)$/ and exists $template{$2};
}
untie @auto_it;

How will it replace the values from hash %template to the auto_it.txt file.

if i have misinterpreted the code then please correct me.

i am a beginner in perl. i have spend one week on this language.

Please let me know some resources and books from where i can learn coding techniques like yours.

Thank your for your help.

Thanks,
Ajay


FishMonger
Veteran / Moderator

Nov 8, 2008, 10:22 AM

Post #19 of 26 (4354 views)
Re: [sh.ajay12] Urgent ( search a file for a line ) [In reply to] Can't Post

Tie::File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on.

Normally, whenever you modify an element of an array that's tied via Tie::File, it will write/save the file at each and every change.

However,
(tied @auto_it)->defer;
tells it to wait until you untie the array then write/save the file.

The for loop is looping over each element in the array (i.e., each line in the file) and preforms a regex that captures the kn and everything before it and saves them in $1 and $2.

If the match was successful, it then checks to see if there is a key in the hash that matches the value in $2.

If there is a $2 hash key, then it changes the value of the array element (i.e., the value of the line in the file) to the concatenated value in $1 and $template{$2}

You can get the details of how to use Tie::File by reading its cpan doc.
http://search.cpan.org/~mjd/Tie-File-0.96/lib/Tie/File.pm

To learn about some of the better known Perl book, run this Perl command.

perldoc -q books


sh.ajay12
User

Nov 8, 2008, 10:23 AM

Post #20 of 26 (4353 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post

Thank you

I am trying to understand every line of your code.

Thanks.
Ajay


KevinR
Veteran


Nov 8, 2008, 10:40 AM

Post #21 of 26 (4349 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post


In Reply To
Please take note on who's responding to you. It's fishMonger, not Kevin.


Not so fast! I was enjoying getting all the credit! Wink Cool
-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 8, 2008, 10:44 AM

Post #22 of 26 (4347 views)
Re: [KevinR] Urgent ( search a file for a line ) [In reply to] Can't Post


In Reply To

In Reply To
Please take note on who's responding to you. It's fishMonger, not Kevin.


Not so fast! I was enjoying getting all the credit! Wink Cool


So, you like credit...would you also like my bills? CrazySly


KevinR
Veteran


Nov 8, 2008, 10:53 AM

Post #23 of 26 (4345 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post


In Reply To

In Reply To

In Reply To
Please take note on who's responding to you. It's fishMonger, not Kevin.


Not so fast! I was enjoying getting all the credit! Wink Cool


So, you like credit...would you also like my bills? CrazySly


Sure, your dollar bills! Wink
-------------------------------------------------


FishMonger
Veteran / Moderator

Nov 8, 2008, 11:22 AM

Post #24 of 26 (4343 views)
Re: [KevinR] Urgent ( search a file for a line ) [In reply to] Can't Post

All I have is Monopoly money, so you'd be in the poor house with me. PirateFrown


KevinR
Veteran


Nov 8, 2008, 11:48 AM

Post #25 of 26 (4341 views)
Re: [FishMonger] Urgent ( search a file for a line ) [In reply to] Can't Post


In Reply To
All I have is Monopoly money, so you'd be in the poor house with me. PirateFrown


I thought I saw you, I live in the poor house too. Wink
-------------------------------------------------

First page Previous page 1 2 Next page Last page  View All
 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives