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

Hi Monks

I have a small script that works, but it would be ideal if I could convert it to a one-liner. The objective is to return the unique values in the {108: tag. So in the example provided 8480963411 and 8481963413 would be returned and stored as 2 separate values in a Shell variable. Not sure it can be done, but thought I'd check with the Monks.

#!/usr/bin/env perl use strict; use warnings; $/ = "{S:{CON:}}"; while (<DATA>) { if ( /\{108\:(\w+)\}\}/ ) { print "$1\n"; } } __DATA__ {1:6392495900}{4:{177:1807300738}{451:1}{405:K22005}{108:8480963411}}{ +1:6392495900}{2:IVSN}{3:{108:8480963411}}{5:{MAC:00000000}{CHK:9F11}} +{S:{CON:}}${1:6392495903}{4:{177:1807300738}{451:1}{405:K22005}{108:8 +481963413}}{1:6392495903}{2:IVSN}{3:{108:8481963413}}{5:{MAC:00000000 +}{CHK:12846}}{S:{CON:}}

I appreciate any and all help

Thanks

Replies are listed 'Best First'.
Re: One Liner
by kcott (Archbishop) on Nov 09, 2018 at 18:58 UTC

    G'day dirtdog,

    "The objective is to return the unique values in the {108: tag."

    The data you've identified as being the unique values (8480963411 and 8481963413) are not unique: both occur twice. Unfortunately, that raises questions about whether: your spec is incorrect; the wrong input data was presented; the expected output was wrong; and so on.

    This gets the expected values you've identified into a shell variable using a one-liner with the input data you've shown.

    $ echo $PM_1225496 $ PM_1225496=`perl -Mstrict -Mwarnings -ne 'BEGIN { $/ = "{S:{CON:}}" +} while (/^.*?\{108\:(\w+)\}\}.*$/g) { print "$1\n" }'` {1:6392495900}{4:{177:1807300738}{451:1}{405:K22005}{108:8480963411}}{ +1:6392495900}{2:IVSN}{3:{108:8480963411}}{5:{MAC:00000000}{CHK:9F11}} +{S:{CON:}}${1:6392495903}{4:{177:1807300738}{451:1}{405:K22005}{108:8 +481963413}}{1:6392495903}{2:IVSN}{3:{108:8481963413}}{5:{MAC:00000000 +}{CHK:12846}}{S:{CON:}} $ echo $PM_1225496 8480963411 8481963413 $

    I'll leave you to decide if that's what you wanted.

    — Ken

      Thank you for replying. By unique, I meant only return the value once even if it's in there multiple times. What you have is perfect. thanks again!

Re: One Liner
by LanX (Saint) on Nov 09, 2018 at 18:47 UTC
    Have a look into perlrun

    flags like l n a p e F are your friend here.

    HTH! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice