Solved

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

Posted on 2008-10-02
10
216 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
quoting a comma separated list 20 93
Non-recursive backtracking, using a stack 1 312
Re-position the objects 7 132
CONDITIONAL MAX STATEMENT IN T-SQL 3 103
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

738 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