Desperately Need Help On This Perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Desperately Need Help On This Perl script
# 15  
Old 10-04-2006
Alternative in Python:
Code:
#!/usr/bin/python
import os
os.chdir("/yourdir")
for files in os.listdir(os.getcwd()):
     for lines in open(files):
          firstquoteposition = lines.find('"')
          secondquoteposition = lines[firstquoteposition + 1:].find('"')
          akamai = lines[ firstquoteposition + 1 : secondquoteposition ]
          fileitsfoundposition = lines.find(":")
          fileitsfound = lines[:fileitsfoundposition]
          print fileitsfound + " : " + akamai

# 16  
Old 10-04-2006
This post is broken into 3 perl scripts which can be put together to possibly help you with a solution.

Here's a perl script to read through a single file and search for the appropriate output. You may have to modify the regular expression if it doesn't work.
Code:
#!/usr/bin/env perl

use strict;

my $infile = "junkfile";
open( IN , "< $infile") or die "Can't open $infile. $!";
my $b_matches;
my $akam;
my $line;
while( $line = <IN> ) {
  chomp $line;
  $b_matches = grep { /akamai/ } $line;
  if ( $b_matches ) {
    ($akam = $line) =~ s/.*("http:\/\/[^"]*").*$/\1/g;
    printf STDOUT "$akam\n";
  }
}
close(IN);

Now here's another perl script. This will read through a single directory ( in this case /etc, but change it to suit your needs ) and print every file in the directory.
Code:
#!/usr/bin/env perl

my $dir;

$dir = "/etc";

opendir( DIR , "$dir") or die "Can't open $dir. $!";

my $file;
while( $file = readdir(DIR) ) {

  printf STDOUT "$file\n";
}
closedir(DIR);

Here's another script to check the filetype of any given file ( I promise I'm going somewhere with this ).
Code:
#!/usr/bin/env perl

my $file;

$file = "/etc/passwd";

&check_filetype( $file );
&check_filetype( "/dev/null" );
&check_filetype( "/dev/console" );
&check_filetype( "/etc" );

sub check_filetype()
{
  $file = shift;
  if ( -d $file ) {
    print "$file is a directory\n";
  } elsif ( -c $file ) {
    print "$file is character special\n";
  } elsif ( -b $file ) {
    print "$file is block special\n";
  } elsif ( -f $file ) {
    print "$file is a regular file\n";
  }
}#end sub check_filetype

You could use these together to write a function which:
  1. Looks at each file in the base directory, and either:
    1. If it's a regular file, use the "grep" command to find your strings
    2. If it's a directory ( nested within the base directory ), have the function recursively call itself on that directory.
# 17  
Old 10-05-2006
guys, thanks a bizifizigazibillion for your help Smilie

sorry i haven't been updating u all quick enough. been swamped. but yes, i am testing these out now and i will let you know which works and which doesn't.


thanks again.
Terrible
# 18  
Old 10-05-2006
Quote:
Originally Posted by anbu23
try this
Code:
grep -r akamai * | perl -ne '$_=~ s/^([^:]*).*(http.*akamai[^"]*).*$/\1: \2/; print $_'


this one seems to work like a charm. the output was precise and it grepped out exactly the http links..granted there were some bogus characters attached to a few lines of the output but hey that can be dealt with.


dont wanna sound like a pain but cna u breake this line down for me so i know what each letter or section does? so i can use this to do other things.
Terrible
# 19  
Old 10-05-2006
Quote:
Originally Posted by nathan
This post is broken into 3 perl scripts which can be put together to possibly help you with a solution.

Here's a perl script to read through a single file and search for the appropriate output. You may have to modify the regular expression if it doesn't work.
Code:
#!/usr/bin/env perl

use strict;

my $infile = "junkfile";
open( IN , "< $infile") or die "Can't open $infile. $!";
my $b_matches;
my $akam;
my $line;
while( $line = <IN> ) {
  chomp $line;
  $b_matches = grep { /akamai/ } $line;
  if ( $b_matches ) {
    ($akam = $line) =~ s/.*("http:\/\/[^"]*").*$/\1/g;
    printf STDOUT "$akam\n";
  }
}
close(IN);


This is splendid. this looks like i can use it and turn it into something massive. but i dont know how to edit this. do i have to change the part that says "junkfile". how do i specifiy the directory for it to search through?
Terrible
# 20  
Old 10-05-2006
Quote:
Originally Posted by Terrible
this one seems to work like a charm. the output was precise and it grepped out exactly the http links..granted there were some bogus characters attached to a few lines of the output but hey that can be dealt with.


dont wanna sound like a pain but cna u breake this line down for me so i know what each letter or section does? so i can use this to do other things.
-n switch tells Perl to execute some code line-by-line, placing each line into the $_ variable

-e switch gives the code to run on the input

$_=~ s/^([^:]*).*(http.*akamai[^"]*).*$/\1: \2/

=~
The right argument is a search pattern, substitution, or transliteration. The left argument is what is supposed to be searched, substituted, or transliterated.

([^:]*)
This will match path of file.\1 will give the string matched by this regular expression

(http.*akamai[^"]*)
This will match the web address.\2 will give the string matched by this regular expression
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

PERL: In a perl-scripttTrying to execute another perl-script that SETS SOME VARIABLES !

I have reviewed many examples on-line about running another process (either PERL or shell command or a program), but do not find any usefull for my needs way. (Reviewed and not useful the system(), 'back ticks', exec() and open()) I would like to run another PERL-script from first one, not... (1 Reply)
Discussion started by: alex_5161
1 Replies

2. Shell Programming and Scripting

Excuting perl script from within a perl script with variables.

Not sure what I am doing wrong here, but I can print the list with no issue. Just a blank screen with the 'do'. #!/usr/bin/perl open FILE, "upslist.txt"; while ($line=<FILE>){ if ($line=~/^(.*?),(.*?)$/){ #print "ups:$1 string:$2\n"; do 'check_snmp_mgeups-0.1.pl -H $1 -C $2'; } ... (1 Reply)
Discussion started by: mrlayance
1 Replies

3. Shell Programming and Scripting

Perl : embedding java script with cgi perl script

Hi All, I am aware that html tags can be embedded in cgi script as below.. In the same way is it possible to embed the below javascript in perl cgi script ?? print("<form action="action.htm" method="post" onSubmit="return submitForm(this.Submitbutton)">"); print("<input type = "text"... (1 Reply)
Discussion started by: scriptscript
1 Replies

4. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

Expect Scripting Loop Argument Desperately Needed!

I am trying to create an Expect script that does the following: 1) Telnets to an IP address and logs in with user ID and Password 2) Issue a CLI command to the server that will output data of which I am particularly interested in a DS1 clock 'Slips' value. I want to be able to keep issuing... (0 Replies)
Discussion started by: dwightlaidler
0 Replies

6. Shell Programming and Scripting

calling a perl script with arguments from a parent perl script

I am trying to run a perl script which needs input arguments from a parent perl script, but doesn't seem to work. Appreciate your help in this regard. From parent.pl $input1=123; $input2=abc; I tried calling it with system("/usr/bin/perl child.pl $input1 $input2"); and `perl... (1 Reply)
Discussion started by: grajp002
1 Replies

7. UNIX for Dummies Questions & Answers

UNIX advice needed desperately :/

Firstly I do need to state that I am turning here as a last resort. Im in my first year of a Computer Networking and technical support course, and as a first year I must complete a Introductory Unix/Linux class. I have been doing well in the course so far however I have been stumped on the final... (1 Reply)
Discussion started by: setaylor5
1 Replies

8. Shell Programming and Scripting

Perl :How to print the o/p of a Perl script on console and redirecting same in log file @ same time.

How can i print the output of a perl script on a unix console and redirect the same in a log file under same directory simultaneously ? Like in Shell script, we use tee, is there anything in Perl or any other option ? (2 Replies)
Discussion started by: butterfly20
2 Replies

9. Shell Programming and Scripting

Help needed desperately

Hello Folks, i have written a script to nullify the log files in my dev environment but the log files are not getting nullified.Please find below my script and also thank you all in advance for your help. find /app -size +100000 -xdev -exec du -k {} \; 2>/dev/null | sort -nr |ls -1dF ... (2 Replies)
Discussion started by: Rayzone
2 Replies

10. Shell Programming and Scripting

Help needed desperately......

Hi guys, I want to execute an SQL script based on a condition.... I am writing a sample requirement... num = {some value} if(num == 0) then cat>> test.sql <<FIN select * from table_a FIN else cat>> test.sql << FIN select * from table_b FIN fi This is what i... (3 Replies)
Discussion started by: Payal Sharma
3 Replies
Login or Register to Ask a Question