Operating System - Linux
1820299 Members
2975 Online
109622 Solutions
New Discussion юеВ

Re: Perl - Simple field delimeter based extraction

 
SOLVED
Go to solution
Daavid Turnbull
Frequent Advisor

Perl - Simple field delimeter based extraction

If I had my Camel book to hand I would hope that I could nut this one out. Basically I need to extract a field, delimeted by '|' (vertical bars) from a single line and use it as a key to grep a line from an other file.

The line might look like:

field number one|field number two|field 3 etc.

I am assuming that this can be done with a simple expression... as opposed to breaking up the string using tokens but my mind is blank as to how this may look syntax wise at the moment.
Behold the turtle for he makes not progress unless he pokes his head out.
6 REPLIES 6
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl - Simple field delimeter based extraction

Sure. Easy

my %keys;
open my $key_file, "while (<$key_file>) {
chomp;
# assuming the key is the first field
my ($key, @fields) = split m/\|/, $_, -1;
$keys{$key} = [ @fields ];

# alternatively, if you don't need the other
# fields, and field 5 is your key
$keys{(split /\|/, $_, -1)[4]}++;
}
close $key_file;

# Now parse the other file(s)
while (<>) {
my @fields = split m/\|/, $_, -1;
# See if field 3 (1-based, perl is 0-based)
# exists in your keys
exists $key{$fields[2]} or next;
# line accepted, process
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Muthukumar_5
Honored Contributor

Re: Perl - Simple field delimeter based extraction

You can try as,

echo "hi|bye|see|you" | perl -ne '@keys=split(/\|/); print @keys;'

Use @keys to grep the line in a file.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Perl - Simple field delimeter based extraction

You can try with a script also as,

#!/usr/bin/perl

# single Line
$_="first|second|third|four|fifth";
@keys=split (/\|/);
$FILE="/tmp/test.log";

open(FH,$FILE) || die "Can not open file: $!";

while () {

foreach $key (@keys)
{
print $_ if ( m/$key/);
}
}

# end #

hth.
Easy to suggest when don't know about the problem!
Daavid Turnbull
Frequent Advisor

Re: Perl - Simple field delimeter based extraction

Thanks for the responses.

All these work and work remarkably well.

I will assign points after I have coded and tested based on what I have learned.
Behold the turtle for he makes not progress unless he pokes his head out.
Daavid Turnbull
Frequent Advisor

Re: Perl - Simple field delimeter based extraction

My app was a little more convoluted but what I learned from your posts was spot on. I assigned 10 points each.
Behold the turtle for he makes not progress unless he pokes his head out.
Daavid Turnbull
Frequent Advisor

Re: Perl - Simple field delimeter based extraction

Problem solved. Next I have some xml to parse with Perl so I may end up asking more questions shortly.
Behold the turtle for he makes not progress unless he pokes his head out.