perl newb issue


 
Thread Tools Search this Thread
Top Forums Programming perl newb issue
# 1  
Old 04-28-2011
perl newb issue

This program is supposed to print the date to a file and then the data it parses out of access.log and instead prints out the data it parses, and then the date. Sry, I've been playing with perl for , oh say, three days now.

Code:
#!/usr/bin/perl -w
use diagnostics;

#This file parses out squids access log for address bar entries to see where users purposefully go

$date=`date`;
$net="192.168.4.";
$counter=10;
$logpath="/var/log/squid/userarchive/";
$ip=$net.$counter;

#The counter is set to the max ipadress +1
while ( $counter <= 14 ) {

#Print the date to each file we're writing to
        open(LOGF,">>",$logpath . $ip);
                print LOGF "$date\n";
                $|++;
        close LOGF;

#Grab the access log and go through it, splitting each line into elements of the array
        open(INFILE,"/var/log/squid/access.log") or die "Can't open /var/log/squid/access.log: $!";
                while ( <INFILE> ) {
                        @array = split(" ",);
                        $ip=$net.$counter;

#Only grab things associated with the current searched for ip and ends with .com .net and so on

                        if (($array[7] =~ /[com|org|net|gov|cc|mil]\/$/)&&($array[3] eq $ip)) {

#Write it to the log file

                                open(LOGF,">>",$logpath . $ip) or die "Can't open $logpath$ip: $!";
                                        print LOGF "$ip  \: $array[0] $array[7]\n";
close LOGF;
                        }
                }
        $counter++;
        }

I open one file, write to it, close it and then run my loop, but it seems to do this backwards. Anyone?
# 2  
Old 04-29-2011
You are always appending, apparently? ">>", so is it new data? http://www.tizag.com/perlT/perlfileopen.php You might just leave the output open until the end. Are you sure the field you want is in [7]? Try logging the whole array 0-length-1 as a debug message.

Last edited by DGPickett; 04-29-2011 at 03:52 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

newb at scripting how would I do this?

Hi guys. I have been lurking for quit some time attempting to learn on my own but I have reached an impasse. I need to extract specific details from from a text file that I get and parse it out into separate files with the name of each individual and the date it was proccess. Then within... (2 Replies)
Discussion started by: lostpacket
2 Replies

2. HP-UX

Newb Help, need to image one HD to another

Hello, I am totally new to Unix We have a piece of test equipment, three of them actually, that run HP-UX 9.0 from like 1994. We had backup tapes with procedures to load the OS and our test software, but the 16-track tapes are corrupt now. HP will not support it, they dont have it anymore,... (11 Replies)
Discussion started by: Newball80
11 Replies

3. Shell Programming and Scripting

Help a newb with sed?

So here is my problem: I need to do a sed search and replace on the following: variable_blah_STATUS replaced with "replace string". sed 's/variable_blah_STATUS/replace string/g' works great but there is a catch. The catch is that I need to check and not replace variable_blah_STATUS... (3 Replies)
Discussion started by: phreezr
3 Replies

4. Shell Programming and Scripting

newb help needed

hi all, im working on a script that needs to read in a file, search for a string of text within that file, and if the string is found, do something. so i know i need to use an if-then statement, but how do i test for the existence of a string within the file? any help would be greatly appreciated. (6 Replies)
Discussion started by: mdlearnest
6 Replies

5. UNIX for Dummies Questions & Answers

Newb here....Someone spying on me?

Hey all. I'm brand spankin' new here to the board. I'm also a novice at Unix. I've been using it for about 1 year now but I haven't really expanded my abilities since I learned the basics. I am in school and I mainly just use Unix to submit stuff to assignment directories and use it to compile my... (3 Replies)
Discussion started by: andre2000
3 Replies

6. UNIX for Dummies Questions & Answers

Ok I'm a Newb, Please Help

I am wanting to download the Linux Program. When i go to download it I see several things to download. What do i download exactly? Then what do I do to install it. Also I have partitioned my hard drive to make way for Linux on the other part to play with it, will any problems arise from this? I... (3 Replies)
Discussion started by: Seeto
3 Replies

7. UNIX for Dummies Questions & Answers

Newb question

I don't even know if this should go here but I just would like to know what this means: d0e45878043844ffc41aac437e86b602 I know absolutely nothin' about UNIX, and I found this in a SQL table in a board I run. Someone please tell me what that is in "normal" mode. Pardon me for my... (4 Replies)
Discussion started by: daeglin
4 Replies

8. UNIX for Dummies Questions & Answers

help for a newb

ok i just installed FreeBSD 4.8 on a computer i had lying around and it was working ok but then when i tried to set up KDE's xdm (kdm) i think i must have configured the login manager wrong because i cant login through the kdm and therefore i can log in to BSD at all?! please can someone help me... (5 Replies)
Discussion started by: h3x
5 Replies

9. UNIX for Dummies Questions & Answers

100% newb - please help

I run windows 2000 right now. I want to learn how to use unix/linux. How do i go about installing Unix/Linux? What's the easiest version to install? Where can i get it? Is my Hardware Compatable? How do i partition my Hd? HELP!!!!!:confused: (6 Replies)
Discussion started by: Rawdawg
6 Replies

10. UNIX for Dummies Questions & Answers

The biggest newb ever...

Hi, all you unix people. I am a pretty advanced windows user, but I am curious about unix. Is there any reason I should attempt to acquire some form of unix for my home computer system? What sort of things is unix useful for? Unix is open source, right? Assuming that to be the case, I infer that... (3 Replies)
Discussion started by: BoneMalone
3 Replies
Login or Register to Ask a Question