Solved

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

Posted on 2008-10-02
10
215 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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