besogon has asked for the wisdom of the Perl Monks concerning the following question:

I have a file looking like this:

field1=value1, field2=value2, field3=value3 field1=value1a, field2=value2a, field3=value3a

need to extract the values only:

value1 value2 value3 value1a value2a value3a

This oneliner works but looks lame:

cat file.txt | perl -ne 'm!.*=(.*),.*=(.*),.*=(.*)!; print "$1 $2 $3\n +"'

Doing it for a 10 field file would be too much. Is there a way to start delimeter using one field and end using a different character? Can array be used?

Please help.

thank you in advance

2017-08-01 Athanasius added code tags

Replies are listed 'Best First'.
Re: one liner function
by Anonymous Monk on Aug 01, 2017 at 04:46 UTC
    @x = /=([^,]*)/g
Re: one liner function
by Laurent_R (Canon) on Aug 01, 2017 at 14:49 UTC
    One possible solution:
    use strict; use warnings; while (my $line = <DATA>) { my @pairs = split /[, ]+/, $line; my @values; push @values, (split /=/, $_)[1] for @pairs; print "@values"; } __DATA__ field1=value1, field2=value2, field3=value3 field1=value1a, field2=value2a, field3=value3a
    This is the output obtained:
    value1 value2 value3 value1a value2a value3a
Re: one liner function
by Laurent_R (Canon) on Aug 01, 2017 at 07:14 UTC
    Hi besogon,

    please use <code> and </code> tags to preserve the formatting of your input file. For the time being, it is not possible to ascertain whether your file is just one line or whether each field=value pair stand on its own line.

Re: one liner function
by AnomalousMonk (Archbishop) on Aug 01, 2017 at 14:09 UTC
    Is there a way to start delimeter using one field and end using a different character?

    I don't understand this question. Can you elaborate a bit? Maybe with some input/output examples?


    Give a man a fish:  <%-{-{-{-<

Re: one liner function
by NetWallah (Canon) on Aug 02, 2017 at 03:24 UTC
    One-liner option:
    perl -anF=\|,\\s -E 'for (my $i=1; $i<=$#F; $i+=2){print $F[$i],qq| +|};print qq|\n|' < file.txt

                    All power corrupts, but we need electricity.