searching a sub-string in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching a sub-string in a string
# 1  
Old 07-22-2011
searching a sub-string in a string

Im trying to download a file from a website.

But i want to download only if the file version is latest than the same file I have.

The file name is CTP-LATEST-5.3.0.37.iso

I need to check the string "37" in the file name.

So if the number 37 is greater than the version Im already having then only I download.

Once I download the file I want to store the string "37" is a file so that I can read it next time when I run the perl code to check for version

Here is my code Im trying with
Code:
#!/usr/bin/perl 
use LWP::Simple; 
 #my $version =37;//this whole line is commented 
Open file version.txt and get the version number(say 37)and store it in $version     
 
 
$pageURL="http://www.google.comisos/psfsS5.3/LATIMGODCVP/";  
 
$simplePage=get($pageURL); 
 
Now $simplePage has the substring  "CTP-LATEST-5.3.0.(some_version_number).iso"  
 
I need to get that "some_version_number". HOW ? 
 
 
if(some_version_numner > $version) 
 
{ 
 
 
#-- fetch the zip and save it as perlhowto.zip 
my $status = getstore("http://www.google.com/isos/psfsS5.3/LATIMGODCVP/CVP-LATEST-5.3.0."$version".iso", "CTP-Latest5.3.0.$version.iso"); 
 
if ( is_success($status) ) 
{ 
 print "file downloaded correctly\n"; 
 
 Store the "some_version_number in file version.txt (for next time use) 
 
} 
else 
{ 
 print "error downloading file: $status\n"; 
 } 
 
} 

Please not that In the website there are other files with .iso extension , But I need the only the file with "CTP-LATEST-5.3.0." prefix

I hope everything is clear in code. The version.txt has only 1 string stored that is any number (say 37 or 45 or 89 etc )

Can someone help me on how to do the rest of the code ?
# 2  
Old 07-22-2011
Tools Substr

Quote:
Originally Posted by srijith
Im trying to download a file from a website.

But i want to download only if the file version is latest than the same file I have.

The file name is CTP-LATEST-5.3.0.37.iso

I need to check the string "37" in the file name.

So if the number 37 is greater than the version Im already having then only I download.

Once I download the file I want to store the string "37" is a file so that I can read it next time when I run the perl code to check for version

Here is my code Im trying with
Code:
#!/usr/bin/perl 
use LWP::Simple; 
 #my $version =37;//this whole line is commented 
Open file version.txt and get the version number(say 37)and store it in $version     
 
 
$pageURL="http://www.google.comisos/psfsS5.3/LATIMGODCVP/";  
 
$simplePage=get($pageURL); 
 
Now $simplePage has the substring  "CTP-LATEST-5.3.0.(some_version_number).iso"  
 
I need to get that "some_version_number". HOW ? 
 
 
if(some_version_numner > $version) 
 
{ 
 
 
#-- fetch the zip and save it as perlhowto.zip 
my $status = getstore("http://www.google.com/isos/psfsS5.3/LATIMGODCVP/CVP-LATEST-5.3.0."$version".iso", "CTP-Latest5.3.0.$version.iso"); 
 
if ( is_success($status) ) 
{ 
 print "file downloaded correctly\n"; 
 
 Store the "some_version_number in file version.txt (for next time use) 
 
} 
else 
{ 
print "error downloading file: $status\n"; 
} 
 
} 

Please not that In the website there are other files with .iso extension , But I need the only the file with "CTP-LATEST-5.3.0." prefix

I hope everything is clear in code. The version.txt has only 1 string stored that is any number (say 37 or 45 or 89 etc )

Can someone help me on how to do the rest of the code ?

Hi,

Try this code,

Code:
my $oldVersion="38";##Version.txt File Version
my $simplePage ="CTP-LATEST-5.3.0.37.iso";
my $str = (split("CTP-LATEST-5.3.0.",$simplePage ))[1];
my $version = substr("$str",0,2);
if($version > $oldVersion )
{
   print "NewVersion\n";
   ####Proceed your further code
}
else
{
   print "old Version\n";
   ####Proceed your further code
}

Cheers,
RangaSmilie

Last edited by pludi; 07-22-2011 at 05:21 AM..
# 3  
Old 07-22-2011
Hi Ranga,
I did not understand some of your lines

1) my $simplePage ="CTP-LATEST-5.3.0.37.iso";

why are you hard coding this ?

I need to get this srting from webpage, get only "37" part and then compare it.
# 4  
Old 07-22-2011
Tools Substr

my $simplePage = get(webpage);

This is your code right??

$simplePage may contains data like SomeContent_CTP-LATEST-5.3.0.37.iso right?
# 5  
Old 07-22-2011
Hi I tried your code this way

Code:
#!/usr/bin/perl
 
use LWP::Simple;
my  $oldversion =36;
 
$pageURL="http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/";   
my $simplePage=get($pageURL);  
#my $simplePage ="CTP-LATEST-5.3.0.37.iso";
my $str = (split("CTP-LATEST-5.3.0.",$simplePage ))[1];
my $version = substr("$str",0,2);
if($version =! $oldVersion )
{
print "$version";
 
##-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/preFCS5.3/LATESTGOODCVP/CTP-LATEST-5.3.0.$version.iso", "CVP-LATEST-5.3.0.$version.iso");
}
else
{
print("Currently new version\n");
}

but its printing $version as 1 , which is not the correct value. The correct value is 37 which its not able to print.
# 6  
Old 07-22-2011
Substr

just print the $simplePage variable and get back me with that value and also send the possibilities.
# 7  
Old 07-22-2011
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /isos/preFCS5.3/LATESTGOODCVP</title>
 </head>
 <body>
<h1>Index of /isos/preFCS5.3/LATESTGOODCVP</h1>
<table><tr><th><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr><tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/back.gif" alt="[DIR]"></td><td><a href="/isos/preFCS5.3/">Parent Directory</a></td><td>&nbsp;</td><td align="right">  - </td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="CVP-LATEST-5.3.0.37.iso">CVP-LATEST-5.3.0.37.iso</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">816M</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="ChangeLog-LATEST.2011-07-19-03h.30m.01s">ChangeLog-LATEST.2011-07-19-03h.30m.01s</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right"> 16K</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="is.iso">is.iso</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">816M</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="md5SUM">md5SUM</a></td><td align="right">19-Jul-2011 03:32  </td><td align="right">111 </td></tr>
<tr><th colspan="5"><hr></th></tr>
</table>
<address>Apache/2.2.3 (Red Hat) Server at www.google.com Port 80</address>
</body></html>

here is the content of my page. You can see the filename marked in red. I want to extract only that file name and then from that filename I want to extract the number "37" or anyother number that is there is that place and then compare with already stored number.

37 may change to 38 or 45 or 98 in future days

Last edited by srijith; 07-22-2011 at 06:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

How to add a new string at the end of line by searching a string on the same line?

Hi, I have a file which is an extract of jil codes of all autosys jobs in our server. Sample jil code: ************************** permission:gx,wx date_conditions:yes days_of_week:all start_times:"05:00" condition: notrunning(appDev#box#ProductLoad)... (1 Reply)
Discussion started by: raghavendra
1 Replies

2. Shell Programming and Scripting

Searching a string in a particular file name

Hello, I have a file name like FIRST_DPF_DAILY_CUST_0826152322.txt i need to extract the string after the third "_" underscore upto timestamp ends i.e CUST_0826152322 can anyone help me with the code Thank you! Regards Srikanth Sagi (3 Replies)
Discussion started by: srikanth_sagi
3 Replies

3. Shell Programming and Scripting

Searching for a string using loop.

Hi i am very new to shell scripting. I have got stuck on a portion on my script. Problem : I have 30 logfiles inside /home/test directory. I need to find the string "@ended today" in all the 30 logfiles and print the name of the files which did not have this string. i need to this by... (3 Replies)
Discussion started by: Soma Das
3 Replies

4. Shell Programming and Scripting

Searching a String

Hi everyone ! suppose i'm searching for a specific string in a file so it is very easy, i use the following command grep 'keyword' file_name but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is... (12 Replies)
Discussion started by: ourned
12 Replies

5. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

6. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

7. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

8. UNIX for Dummies Questions & Answers

Searching for a string variable

Hi ... I have a string variable STR = "This is a test message" I have a file abc.txt that I am searching for the occurence of the string STR ... I am using the command in a script cat abc.txt | grep $STR It identifies each space as a seperator and prints word by word. How to... (2 Replies)
Discussion started by: mattrix
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Dummies Questions & Answers

searching for a string in directory

Hi, I have a directory with a couple of thousand logs in it. The log files are created every 5 minutes. I want to search these logs grep for a specific sting and more importantly print the name of the files where that sting was found. e.g. all logs begin om20020927 what I have been... (4 Replies)
Discussion started by: warrend
4 Replies
Login or Register to Ask a Question