Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-02
10
Medium Priority
?
221 Views
Last Modified: 2008-10-03
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
0
Comment
Question by:matgold
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22627475
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

0
 

Author Comment

by:matgold
ID: 22628360
are you sure about the matching, I'm not getting anything
0
 

Author Comment

by:matgold
ID: 22628803
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 39

Expert Comment

by:Adam314
ID: 22629080
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?
0
 

Author Comment

by:matgold
ID: 22630638
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.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 200 total points
ID: 22630947

#!/usr/bin/perl
use strict;
my %hosts;
while(<>) {
        next unless /^host:\s*(.*)/;
        $hosts{$1}++;
}
 
foreach (sort {$hosts{$a} <=> $hosts{$b}} keys %hosts) {
    print "$_: $hosts{$_}\n";
}

Open in new window

0
 

Author Comment

by:matgold
ID: 22634224
perfect!
Thank You very much
0
 

Author Comment

by:matgold
ID: 22635038
currently it is sorting by total count, how can I sort by host name.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22635138
Change line 9 to:
    foreach (sort keys %hosts) {
0
 

Author Comment

by:matgold
ID: 22635277
fantastic, thanks again
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question