04-07-2009
rikxik..
thanks for the brilliant suggestion given..i also thought the problem might be with the programming (foreach statement) as i'm not into much of the programming..
i've review the code and cut out the unnecessary loop..it works and it works great if i execute it from the terminal instead of browser..
thanks insamniac for ur ksh opinion..i would really like to consider it but i don't really have much time now to learn new things..but i currently trying the code already..it surely gives me another sight way of solving coming problems..
Quote:
#!/usr/bin/perl -w
use File::Basename;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use File::Path;
use File::Copy;
print "Content-type: text/html\r\n";
print "\r\n";
$accessfile = "/var/log/foo/access_log";
open (example7, "/var/log/foo/access_log") || die ("Could not open file. <br> $!");
@cisco = <example7>;
@passwd = split(' ',$new);
open(READER, $accessfile) or die "canot read log:$accessfile:$!";
@LOGFILE = <READER>;
while(<READER>) {
($machine, $date, $time, $tzone, $method, $url, $httpvers, $response, $size) = /([^\s]*) [^\s]* [^\s]* \[([^\:]*)
[^\s]*) ([^\]]*)\] \"([^\s]*) ([^\s]*) HTTP\/([^\"]*)\" (\d*) (\d*)/;
($day, $month, $year) = split('/',$date);
}
foreach $string (sort(@passwd)) {
my $path = "/path/foo/foo/$string/log";
if (! -d $path)
{
mkpath($path,755) or die "Failed to create $path: $!\n";
}
$file = "$string.txt";
if ($path =~ $string) {
foreach $line (sort(@cisco)) {
$append = 0;
if ($append){
open (OUTFILE,">$file"); if ($line =~ $string) {print OUTFILE $line;}
}else{
open (OUTFILE,">>$file"); if ($line =~ $string) {print OUTFILE $line;}
} }
close (OUTFILE);
move($file,$path) or die "Failed to copy $file: $!\n";
}
}
close (example7);
Last edited by meggae; 04-07-2009 at 12:44 AM..
10 More Discussions You Might Find Interesting
1. UNIX for Advanced & Expert Users
All,
Is there any way out to display the nth line before the string is matched ???
Eg : If i have a file which has the following contents and if i want to get the
3rd line before the string is matched
a
b
c
d
e
f
if i give the input as f and lines before the match as 3 then it should... (5 Replies)
Discussion started by: helper
5 Replies
2. Shell Programming and Scripting
Hi
I want to read a file line by line and search for a particular string in each line(say for example string containing @ )and save that string into a variable.
Can someone suggest me the way to implement it.I am using K- shell
Thanks
Ishita (5 Replies)
Discussion started by: Ishita
5 Replies
3. Shell Programming and Scripting
Hi ,
I have input file and i want to extract below strings
<msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets
<resCode>3000</resCode> => 3000 needs to be extracted
<resMessage>Request time
getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies
4. UNIX for Advanced & Expert Users
Hi,
I have a query as follows :
suppose I am matching a string in a file say "start from here"
and I want to pick up 'n' number of lines () from the matched string. Is there any way to do that ?
1) going forward I want to do this for every match for the above string
2) or limit this to... (2 Replies)
Discussion started by: sumoka
2 Replies
5. Shell Programming and Scripting
Hi Experts,
I am very new to scripting and have a prb since few days and it is urgent to solve so much appreciated if u help me.
i have 2 files
file1.txt
9647810043118
9647810043126
9647810043155
9647810043161
9647810043166
9647810043185
9647810043200
9647810043203
9647810043250... (22 Replies)
Discussion started by: mustafa.abdulsa
22 Replies
6. Shell Programming and Scripting
Hi Experts,
Help needed on joining one line above & below to the pattern matched string line.
The input file, required output is mentioned below
Input file
ABCD DEFG5 42.0.1-63.38.31
KKKK iokl IP Connection Available
ABCD DEFG5 42.0.1-63.38.31
... (7 Replies)
Discussion started by: krao
7 Replies
7. Shell Programming and Scripting
Hi,
I have a file whose lines are something like
Tchampionspsq^@~^@^^^A^@^@^@^A^A^Aÿð^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^?ð^@^@^@^@^@^@^@?ð^@^@^@^@^@^@pppsq^@~^@#@^@^@^@^@^@^Hw^H^@^@^@^K^@^@^@^@xp^At^@^FTtime2psq^@ ~^@^^^A^@^@^@^B^A
I need to extract all words matching T*psq from the file.
Thing is... (4 Replies)
Discussion started by: shekhar2010us
4 Replies
8. Shell Programming and Scripting
I have a log file as given below
012/01/21 10:29:02 (111111) Processing Job '23_369468343464564'
2012/01/21 10:29:02 (111111)
Making Job '23_369468343464564.0'...
2012/01/21 10:29:04 (111111)
Jobnumber '23_369468343464564' was successful
2012/01/21 10:29:04 ... (12 Replies)
Discussion started by: rpm120
12 Replies
9. UNIX for Dummies Questions & Answers
Hi,
I would like to comment a line that matched a string "sreenivas" in a file without opening it.
Thanks in advance.
Regards,
Sreenivas (3 Replies)
Discussion started by: raosr020
3 Replies
10. Shell Programming and Scripting
i am having file like this
#!/bin/bash
read -p 'Username: ' uservar
match='<color="red" />'
text='this is only a test
so please be patient
<color="red" />'
echo "$text" | sed "s/$match/&$uservar\g"
so desireble output what i want is if user type MARIA
this is only a test
so please... (13 Replies)
Discussion started by: tomislav91
13 Replies
LEARN ABOUT MOJAVE
path::class5.18
Path::Class(3) User Contributed Perl Documentation Path::Class(3)
NAME
Path::Class - Cross-platform path specification manipulation
VERSION
version 0.33
SYNOPSIS
use Path::Class;
my $dir = dir('foo', 'bar'); # Path::Class::Dir object
my $file = file('bob', 'file.txt'); # Path::Class::File object
# Stringifies to 'foo/bar' on Unix, 'fooar' on Windows, etc.
print "dir: $dir
";
# Stringifies to 'bob/file.txt' on Unix, 'bobfile.txt' on Windows
print "file: $file
";
my $subdir = $dir->subdir('baz'); # foo/bar/baz
my $parent = $subdir->parent; # foo/bar
my $parent2 = $parent->parent; # foo
my $dir2 = $file->dir; # bob
# Work with foreign paths
use Path::Class qw(foreign_file foreign_dir);
my $file = foreign_file('Mac', ':foo:file.txt');
print $file->dir; # :foo:
print $file->as_foreign('Win32'); # foofile.txt
# Interact with the underlying filesystem:
# $dir_handle is an IO::Dir object
my $dir_handle = $dir->open or die "Can't read $dir: $!";
# $file_handle is an IO::File object
my $file_handle = $file->open($mode) or die "Can't read $file: $!";
DESCRIPTION
"Path::Class" is a module for manipulation of file and directory specifications (strings describing their locations, like
'/home/ken/foo.txt' or 'C:WindowsFoo.txt') in a cross-platform manner. It supports pretty much every platform Perl runs on, including
Unix, Windows, Mac, VMS, Epoc, Cygwin, OS/2, and NetWare.
The well-known module File::Spec also provides this service, but it's sort of awkward to use well, so people sometimes avoid it, or use it
in a way that won't actually work properly on platforms significantly different than the ones they've tested their code on.
In fact, "Path::Class" uses "File::Spec" internally, wrapping all the unsightly details so you can concentrate on your application code.
Whereas "File::Spec" provides functions for some common path manipulations, "Path::Class" provides an object-oriented model of the world of
path specifications and their underlying semantics. "File::Spec" doesn't create any objects, and its classes represent the different ways
in which paths must be manipulated on various platforms (not a very intuitive concept). "Path::Class" creates objects representing files
and directories, and provides methods that relate them to each other. For instance, the following "File::Spec" code:
my $absolute = File::Spec->file_name_is_absolute(
File::Spec->catfile( @dirs, $file )
);
can be written using "Path::Class" as
my $absolute = Path::Class::File->new( @dirs, $file )->is_absolute;
or even as
my $absolute = file( @dirs, $file )->is_absolute;
Similar readability improvements should happen all over the place when using "Path::Class".
Using "Path::Class" can help solve real problems in your code too - for instance, how many people actually take the "volume" (like "C:" on
Windows) into account when writing "File::Spec"-using code? I thought not. But if you use "Path::Class", your file and directory objects
will know what volumes they refer to and do the right thing.
The guts of the "Path::Class" code live in the Path::Class::File and Path::Class::Dir modules, so please see those modules' documentation
for more details about how to use them.
EXPORT
The following functions are exported by default.
file
A synonym for "Path::Class::File->new".
dir A synonym for "Path::Class::Dir->new".
If you would like to prevent their export, you may explicitly pass an empty list to perl's "use", i.e. "use Path::Class ()".
The following are exported only on demand.
foreign_file
A synonym for "Path::Class::File->new_foreign".
foreign_dir
A synonym for "Path::Class::Dir->new_foreign".
tempdir
Create a new Path::Class::Dir instance pointed to temporary directory.
my $temp = Path::Class::tempdir(CLEANUP => 1);
A synonym for "Path::Class::Dir->new(File::Temp::tempdir(@_))".
Notes on Cross-Platform Compatibility
Although it is much easier to write cross-platform-friendly code with this module than with "File::Spec", there are still some issues to be
aware of.
o On some platforms, notably VMS and some older versions of DOS (I think), all filenames must have an extension. Thus if you create a
file called foo/bar and then ask for a list of files in the directory foo, you may find a file called bar. instead of the bar you were
expecting. Thus it might be a good idea to use an extension in the first place.
AUTHOR
Ken Williams, KWILLIAMS@cpan.org
COPYRIGHT
Copyright (c) Ken Williams. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
Path::Class::Dir, Path::Class::File, File::Spec
perl v5.18.2 2017-10-06 Path::Class(3)