Solved

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

Posted on 2008-10-02
10
209 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now