CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Pattern search in txt file and output them with comma seperated value

 



liton79
Novice

Sep 18, 2013, 5:29 PM

Post #1 of 9 (11598 views)
Pattern search in txt file and output them with comma seperated value Can't Post

Hi,

I have a file which contains following fields Application, Region and Server with their respective values like ABC, NA & Atom.prod. These fields are repeated to the file only the values are changed each time. I am trying to write a perl program that will read the entire txt fie and and search for the pattern Application, Region, Server and output these patterns and their values something like:

Application,ABC

Region,NA

Server,Atom.prod



The problem I am facing is that since these fields and values are in separate lines I am not able to put the output together in one line with , comma separated. Can someone please help me on this? FYI I am trying to use hash table for the output.



Input.txt file:

========



Details



Application



ABC

Region



NA


Zhris
Enthusiast

Sep 18, 2013, 11:05 PM

Post #2 of 9 (11592 views)
Re: [liton79] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Hi,

This should work with your example input data:


Code
#!/usr/bin/perl 
use strict;
use warnings FATAL => qw( all );

#$/ = "\r\n";
$" = ", ";

my $filepath = 'input.txt';
my $key = undef;
my $hash = { };

# parse
open my $filehandle, '<', $filepath || die "cannot open $filepath: $!";
while ( <$filehandle> )
{
chomp;

if ( /^\s*(application|region|server)\s*$/i )
{
$key = lc $1;
}
elsif ( defined $key && /^\s*(.+)\s*$/ )
{
push @{$hash->{$key}}, $1;
}
}
close $filehandle;

# print
while ( my ( $key, $vals ) = each( %$hash ) )
{
print "$key, @$vals\n";
}



Code
region, NA  
application, ABC


Chris


(This post was edited by Zhris on Sep 18, 2013, 11:13 PM)


2teez
Novice

Sep 28, 2013, 11:04 AM

Post #3 of 9 (11488 views)
Re: [liton79] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Hi, liton79,

You can do like so

Code
use warnings; 
use strict;

my %data;
@data{qw(Application Region Server)} = undef;
my $key;

while (<DATA>) {
chomp;
next if /^$/;
if ( exists $data{$_} ) {
$key = $_;
}
else {
push @{ $data{$key} }, $_ if defined $key;
}
}

while ( my ( $key, $value ) = each %data ) {
print $key, ' ', join( ", " => @$value ), $/;
}

__DATA__


Details


Application

ABC

Region



NA

Server



Atom.prod

Application



NA


Region


XYZ

Server



Atom.prod2

You get this:

Code
Server Atom.prod, Atom.prod2 
Application ABC, NA
Region NA, XYZ



Zhris
Enthusiast

Sep 28, 2013, 7:12 PM

Post #4 of 9 (11480 views)
Re: [2teez] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Hi 2teez,

I like your solution, your Perl has improved a lot in a short time!

In order to get your code to work I also had to strip off the whitespace at the end of each line ( s/\s*$//; ). Was this not an issue for you when testing.

I opted for the compact / regexp approach, just to be less strict with regards to the input data format, in case the OP's real world data required considerable trimming / casing.

The OP actually contacted me with regards to this, and presented a more comprehensive input data example, which neither of our scripts covers. Certain key / value groups exist on the same line and some additional formatting was required. It also wasn't suitable to group values in a list ( there wasn't enough information provided to know how to handle this potential situation ) since "repeats" needed to be handled in a completely separate record, if that makes sense Crazy.

Chris


(This post was edited by Zhris on Sep 28, 2013, 7:18 PM)


Zhris
Enthusiast

Sep 28, 2013, 7:34 PM

Post #5 of 9 (11466 views)
Re: [2teez] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Uhhh, ignore my patronizing comment "your Perl has improved a lot in a short time! ". I got the wrong guy! I mixed you up with one of the beginners, who was making critical mistakes last week, and I was shocked at the improvement.

Chris


2teez
Novice

Sep 28, 2013, 7:43 PM

Post #6 of 9 (11463 views)
Re: [Zhris] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Hi Zhris,

Quote
The OP actually contacted me with regards to this, and presented a more comprehensive input data example, which neither of our scripts cover

I should have mentioned it that I don't know how the OP dataset were really formed, but taking a clue at what has been done, one can suggest that such and such be gone or taken into consideration..

My major problems in forum like this like many others is that OP don't border to give feed backs.


(This post was edited by 2teez on Sep 28, 2013, 7:44 PM)


Zhris
Enthusiast

Sep 28, 2013, 8:20 PM

Post #7 of 9 (11458 views)
Re: [2teez] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

Hi 2teez,

I understood that we only had the example input data to work from. I wanted to share with you the gist of what happened once the OP provided feedback via PM, particularly as you may not receive feedback yourself, giving closure to this potentially open ended thread ( inadvertently alleviating your forum pet hate Wink ).

Its a common issue that the OP doesn't supply enough or the correct portion of data in order to cover every possible scenario, and if they do feedback, it can often take much discussion to eventually come up with the appropriate solution.

This OP has some difficult input data to deal with and was able to provide detailed requirements in later discussion.

Chris


(This post was edited by Zhris on Sep 28, 2013, 8:26 PM)


2teez
Novice

Sep 28, 2013, 8:31 PM

Post #8 of 9 (11453 views)
Re: [Zhris] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post


Quote
This OP has some difficult input data to deal with and was able to provide detailed requirements in later discussion.

Ok, no problems.


Zhris
Enthusiast

Sep 28, 2013, 8:56 PM

Post #9 of 9 (11445 views)
Re: [2teez] Pattern search in txt file and output them with comma seperated value [In reply to] Can't Post

I'll point him back here, since its an ongoing discussion, and he will benefit from everyones time / help. My latest solution is rough at best!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives