Link to home
Start Free TrialLog in
Avatar of matgold
matgold

asked on

How can I extract job_name and host_name from a text file?

I have a text file that contain info about each job. I need to extract job_name and host_name.
The format is as follow. as you can see, each job is seperated by /* ----------------- JOB_NAME ----------------- */

I need to extract "JOB_NAME" and host: yahoo
the output should be like so.
JOB_NAME, host: yahoo
JOB_NAME, host: yahoo
JOB_NAME, host: yahoo
JOB_NAME, host: yahoo


/* ----------------- JOB_NAME ----------------- */
blah
blah
host: yahoo
blah
blah

/* ----------------- JOB_NAME ----------------- */
blah
blah
host: yahoo
blah
blah
Avatar of Adam314
Adam314

Put this in a file, pass it the name of the text file.  eg:
    script.pl your_text_file.txt

#!/usr/bin/perl
use strict;
my $job;
while(<>) {
	$job=$1 if m|/\*\s*\-+\s+(\S+)|;
	print "$job, host: $1\n" if /host:\s*(.*)/;
}

Open in new window

Avatar of matgold

ASKER

are you sure about the matching, I'm not getting anything
Avatar of matgold

ASKER

Adam,

Can we do this instead. I'm going to extract the first five characters.
if it equal "host:" then 1 to rec-cnt.
but I need to be able to add up a total for each type of machine.
How can I do that.
When I run the posted script on the data you gave, i get this exact output:
JOB_NAME, host: yahoo
JOB_NAME, host: yahoo


If your real data looks different, post your real data (if necessary, replace confidential data with similar data - numbers with numbers, letters with letters...).


To check if the first five characters are "host:", you could do  /^host:/.  Add i at the end to ignore case  /^host:/i.

>>if it equal "host:" then 1 to rec-cnt.
>>but I need to be able to add up a total for each type of machine.
Not sure what you mean by this.  Where do you get the type of machine?
Avatar of matgold

ASKER

I'm sorry, I meant host not machine.
its not always going to be yahoo, it could be anything.
since I don't know what's out there, is there a way total up for each type of host.
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matgold

ASKER

perfect!
Thank You very much
Avatar of matgold

ASKER

currently it is sorting by total count, how can I sort by host name.
Change line 9 to:
    foreach (sort keys %hosts) {
Avatar of matgold

ASKER

fantastic, thanks again