perl questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl questions
# 1  
Old 09-24-2007
perl questions

New to perl and a few questions - usually used to shell scripting but have to write a script to check a few things on windows server and need help..

1. Check for file missing in sequence..

ie. list of file in directory as follows:

file0001.txt
file0002.txt
file0004.txt
file0005.txt
file0006.txt

How do I identify that file0003.txt is missing in the sequence ?

2. search files for a pattern and count the matching occurances?

ie. file1 contains
test
test2
test4
test2
test1

I want to know how to search for test2 and count number of matches.

I would prefer if these were as simple as possible.. simple questions I am sure but I havent used perl before..
# 2  
Old 09-24-2007
1. If your files are strictly numbered like that, you can do a readdir() to get the list of files in the directory, sort it, and then keep a running counter that increments the counter by 1 each time. If it matches the next item read, then there is no missing file. I can think of a couple more options, but this one is probably the easiest.

2. The grep() function will do it.

grep - perldoc.perl.org
# 3  
Old 09-25-2007
Quote:
Originally Posted by frustrated1
New to perl and a few questions - usually used to shell scripting but have to write a script to check a few things on windows server and need help..

1. Check for file missing in sequence..

ie. list of file in directory as follows:

file0001.txt
file0002.txt
file0004.txt
file0005.txt
file0006.txt

How do I identify that file0003.txt is missing in the sequence ?
since you are working in windows, here's a vbscript, if you don't mind
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("c:\temp1")
Dim counter
Dim nStore()
counter=1
i =0
fileCount=objFolder.Files.Count
'Store file numbers into array
For Each myFile In objFolder.Files     
	If Left(myFile.Name,4) = "file" And objFSO.GetExtensionName(myFile.Name) = "txt" Then
		m = Right(objFSO.GetBaseName(myFile),4)
		ReDim Preserve nStore(i)
		nStore(i) = m		
		i = i+ 1
	End If 
Next
'go through array, filter out those not in array
For j = 0 To fileCount+1    
	n = String(3,"0") & CStr(counter) 
	MyIndex = Filter(nStore, n)
	If UBound(MyIndex) = -1 Then
		WScript.Echo "file " , n , "not there"
	End If 
	counter=counter+1
Next

ussage on command prompt:
Code:
c:\pathofyourscript> cscript  myscript.vbs


2. search files for a pattern and count the matching occurances?

ie. file1 contains
test
test2
test4
test2
test1

I want to know how to search for test2 and count number of matches.

I would prefer if these were as simple as possible.. simple questions I am sure but I havent used perl before..[/QUOTE]
Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
myFile = "c:\temp1\file0001.txt"
Set objFile = objFSO.OpenTextFile(myFile,1)
c = 0
Do Until objFile.AtEndOfLine
	line=objFile.ReadLine
	If InStr(1,line,"test2") >0 Then
		c=c+1
	End If
Loop
objFile.Close
WScript.Echo "total count: ", c

# 4  
Old 09-25-2007
1. If your files are strictly numbered like that, you can do a readdir() to get the list of files in the directory, sort it, and then keep a running counter that increments the counter by 1 each time. If it matches the next item read, then there is no missing file. I can think of a couple more options, but this one is probably the easiest.

Thanks - can you help me on how to use readdir and increment counter etc and identify that I am missing one?

As I say - I am new to perl so know only the very basics..

Separately I will have a look at the grep function..
# 5  
Old 09-25-2007
Quote:
Originally Posted by frustrated1
2. search files for a pattern and count the matching occurances?
Check this perl script:
Code:
#!/usr/bin/perl
# count_this.pl 
use strict;
my $to_search = shift;
my $count = 0;
while (<>) {
    while (m/$to_search/g) {
        $count++;
    }
}
print "$to_search found $count times \n";

Run this script as:
Code:
[ysawant@in-gcs-nas1 temp]$ perl count_this.pl Name sample.xml 
Name found 6 times 
[ysawant@in-gcs-nas1 temp]$

# 6  
Old 09-30-2007
Can someone help me with the below - script to check for missing file sequence??


1. If your files are strictly numbered like that, you can do a readdir() to get the list of files in the directory, sort it, and then keep a running counter that increments the counter by 1 each time. If it matches the next item read, then there is no missing file. I can think of a couple more options, but this one is probably the easiest.

Thanks - can you help me on how to use readdir and increment counter etc and identify that I am missing one?

As I say - I am new to perl so know only the very basics..
# 7  
Old 09-30-2007
Code:
opendir(DIR, "R:/example") or die "Cannot opendir: $!";
my @files = sort map { /^file\d+\.txt$/ ? ($_) : () } readdir(DIR);
closedir(DIR);

my $min=($files[0] =~ /^file(\d+)\.txt$/ && $1) or die "No matching files found";
my $max=($files[-1] =~ /^file(\d+)\.txt$/ && $1) or die "No matching files found";

my $thisfileidx = 0;
for (my $i=$min; $i<=$max; $i++) {
	if ($files[$thisfileidx] eq sprintf("file%04d.txt", $i)) {
		$thisfileidx++;
	} else {
		print STDERR "File " . sprintf("file%04d.txt", $i) . " is missing!\n";
	}
}

(This is a Windows machine and R:\example is a directory on a ramdisk with file0001.txt, file0002.txt, file0003.txt, file0005.txt and file0006.txt)

It will tell you file0004.txt is missing.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 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

Perl Debug Stepping Answering Questions

I am new to perl and want to get a little better understanding of debugging code in perl. I have a perl script that has questions to be answered like: he following PERL modules are recommended: Crypt::DES Crypt::PasswdMD5 IO::Pty Net::Write::Layer2 String::CRC32 Attempt to install... (0 Replies)
Discussion started by: metallica1973
0 Replies

3. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

4. Shell Programming and Scripting

Perl newbie questions!

Hi, So I started to learn perl a few days ago, and I have some problems... One of my problems... #!C:\Perl64\bin\perl.exe -w use LWP::Simple; print "Content-Type: Text/Plain\n\n"; sub pagelinks { return @all = get($_) =~ /href\s*=\s*"?(+)/gis; } @a =... (5 Replies)
Discussion started by: byte1918
5 Replies

5. Shell Programming and Scripting

How to answer the Questions in Perl .

I need to write a script to telnet or ssh a device the execute some commands in device then copy the output in a file, I wrote the script but I faced one issue, when you execute some commands the device asked me a Question, for example : device # copy run tftp device # Source filename ? ... (3 Replies)
Discussion started by: DarkSoul
3 Replies

6. Shell Programming and Scripting

Perl questions - more

More questions for Perl on Windows (again I apologize its on windows... ) 1. How can I check free disk space on a drive in windows using perl command in a script? 2. How can I check processes running using perl command (as I would normally be able to see in task manager for example) 3. I... (7 Replies)
Discussion started by: frustrated1
7 Replies

7. Shell Programming and Scripting

chess perl program questions

Hello guys, While going over the book, I ran into this chess program and I have few questions 1) on line 40), why is not $chessboard-> ) ??? 40 unless (defined $chessboard->) { 2) 20 foreach my $i (reverse (0..7)) { #Row 1 #!/usr/bin/perl -w 2 # 3 # 4 5 ... (1 Reply)
Discussion started by: hankooknara
1 Replies

8. Shell Programming and Scripting

Perl: answering automatically to install questions

Hi everybody, I have been looking for an answer to this issue both on google and on the forum, but I couldn't find anything. please help me :eek: As part of an automated (in perl) install of Solaris 9, I would like to be able to answer automaticaly to the question the installer asks.... (2 Replies)
Discussion started by: zaap
2 Replies

9. Shell Programming and Scripting

bash and Perl interaction questions

hi. i´m working in bash and am trying to create a Perl daemon that controls bash´s behavior. this is actually in preparation for a later project i´ll be working on. basically, i´m looking for a way to have the Perl daemon tell bash what to do. i already have a small daemon that simply prints... (2 Replies)
Discussion started by: deryk
2 Replies
Login or Register to Ask a Question