Solved

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

Posted on 2008-10-02
10
213 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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 50 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 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