- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- PERL - help
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 09:18 AM
02-24-2005 09:18 AM
PERL - help
I am writing a perl script which selects some pattern from input files.
I have used:
$host=$1 if (/Host name:\s+(\w+)/);
To fetch the host name from the log files.
But I find that in some cases the file does not contain the "Host name". In such cases I have to use the name of the directory under which the files are present to represent the host name.
Can anyone please help me. I am very new to PERL and don't have much skills in it.
Thanks,
Rahul
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 10:21 AM
02-24-2005 10:21 AM
Re: PERL - help
You can then use dig or nslookup to get the hostname, again using awk to parse the output.
example
ipaddy=$(cat logfile | awk '{print $1}')
What this does is pull out the first field in the log, space delimited and puts it in a variable called ipaddy.
awk can use different field delimiters with this add on command.
awk -F:
That will use : as a field delimter.
If you post an example of what the log looks like, I or others will happily write the script for you.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 11:19 AM
02-24-2005 11:19 AM
Re: PERL - help
The input file looks like this:
Host name: mccap2
Time zone: 18000 EST EDT
UTC time: 2005-02-24T04:56:56.550
Local time: 2005-02-23T23:56:56.550
Start time (UTC): 2004-09-28T14:59:32
Running time: 148:13:57:24
...........
I actually have written a PERL script to do the pattern search. It's not only searching for the "Host name" but also lots of other patterns.
But the problem is that some of the log files (very few of them) does not have the "Host name" field in them.
All these log files are stored in directories which are named after there host name. For instance the above file is taken from the directory "mccap2".
So, first I need to verify if the file contains the "host name". If it does not I have to take this field from the name of the directory which contains the file.
Hope my requirement is clear.
Thanks,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 03:16 PM
02-24-2005 03:16 PM
Re: PERL - help
Well, do you get that directory passed in as an argument? Do you 'find' it? Do you 'glob' your way through a list?
For example, let's assume the code looks like:
foreach (glob "*/x.log") {
open (LOG, "<$_") or die ...
while
:
$host=$1 if (/Host name:\s+(\w+)/);
:
}
Then you'd fix that along these lines:
foreach (glob "*/x.log") {
($dir, $file) = split ("\/");
$host = $dir; # pre-set
open (LOG, "<$_") or die ...
while
:
$host=$1 if (/Host name:\s+(\w+)/);
:
}
So just preset $host with the directory name, and if you find the real McCoy, then it will overwrite the directory provided value.
You could also do it after the loop that processes the log with something like
:
undef $host;
(or $host = "")
:
while (
:
}
$host = $dir unless $host;
Hope this helps some!
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2005 08:02 PM
02-24-2005 08:02 PM
Re: PERL - help
# Search all files with mccap*
for dir in `find $PWD -type d -name "mccap*"`;
do
grep -q 'Host name:' $dir/*;
# Host name is there
if [[ $? -eq 0 ]];
then
echo "Hostname is available in $dir/$(ls $dir)";
echo "Hostname is $(awk '/Host name/ { print $3 }' $dir/*)";
echo
# Host name is not there then print base directory name
else
echo "Hostname is not available in $(ls $dir)";
echo "Hostname is $(echo $dir | awk -F "/" '{ print $NF }')"
echo
fi;
done
O/P
Hostname is available in /home/muthu/mccap2/file
Hostname is mccap2
Hostname is not available in file
Hostname is mccap1
HTH.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2005 07:13 AM
02-25-2005 07:13 AM
Re: PERL - help
My script looks like this:
while (<@ARGV>){
if (/\.(\d+)_.*.log/) {
$date = $1;
open (INP, "<$_") or die "Could not open file '$_' for input";
while(
$host=$1 if (/Host name:\s+(\w+)/);
....................
....................
print join(",",$host,$date,$name,$num),"\n";
}
}
Whenever, the Host name is not there in the input files, it is just coming as blank. SO, I want the name of the directory to be used for the host name when the host name is missing.
I tried using:
while (<@ARGV>){
($dir, $file) = split ("\/");
$host = $dir; # pre-set
....
But I got the name of the file in the host field. I want the name of the directory to be in the host.
Can you please help.
Thanks
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2005 08:56 AM
02-25-2005 08:56 AM
Re: PERL - help
use File::Basename
while (<@ARGV>) {
($fname, $fpath)=fileparse($_);
$host = $fpath if (!length $host);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2005 09:31 AM
02-25-2005 09:31 AM
Re: PERL - help
I am getting the follwoing erro while using Basename:
Can't locate File/Basename.pm in @INC (@INC contains: /opt/perl5/lib/5.00502/PA-RISC1.1 /opt/perl5/lib/5
.00502 /opt/perl5/lib/site_perl/5.005/PA-RISC1.1 /opt/perl5/lib/site_perl/5.005 .) at repext.pl line 5.
Is there any other way to accomplish my requirements.
Thanks,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2005 05:35 PM
02-25-2005 05:35 PM
Re: PERL - help
Start your perl script with:
use Cwd;
Then while in the loop print out cwd.
See how you can use that to find the current dir. Possibly you may need something like:
use Cwd;
use File::Spec;
:
loop
:
$absdir = File::Spec->catdir(cwd,$_);
$host = substr($absdir,rindex($absdir,"/")+1);
:
untested!
hth,
Hein.
File::Spec->catdir(cwd