On 08/04/2017 02:13 PM, Brandon Allbery wrote:
(back on the desktop for the moment...)

On Fri, Aug 4, 2017 at 5:04 PM, ToddAndMargo <toddandma...@zoho.com <mailto:toddandma...@zoho.com>> wrote:

    On 08/04/2017 01:00 PM, ToddAndMargo wrote:

        How do I do this with a perl one liner?

        $ echo "a b c d" | awk '{print $2}'

    $ echo "a b c d" | perl6 -n -e 'say lines ~ "\n" ~ .words[2];'
    ( c)

    without "-n" ".words" doesn't work.

    With "-n" "lines" doesn't work

Yes, you've been given a number of things at cross purposes with each other.

So: if you use -n or -p then your expression (via -e) will be run as if it were wrapped in

     for $*IN.lines {
       < your -e here >
       $_.print; # only if you used -p

That is, if you use -n, you do not want to use lines; your code already runs on each line. If this is not what you want then you don't want -n or -p. And if you want to use words in that case, you need to invoke it on whatever string you are trying to split into words (the default is $_, which will be the current line of input with -n or -p).

This leaves me confused as to what `lines ~ "\n" ~ .words[2]` would be doing, though. Read all the input into lines, concatenate them all, append a newline, then try to read *more* input and split to words? Because that's what you wrote (or would have written without -n; with -n, you have read the first line into $_, where words will find it, and lines will eat the remaining lines of input).

What exactly are you trying to do here?


Warning: the following may cause brain damage.

I am looking to extract interface name, IP, and short

$ ip -o -f inet addr show
1: lo inet scope host lo\ valid_lft forever preferred_lft forever 3: enp7s0 inet brd scope global dynamic enp7s0\ valid_lft 155558sec preferred_lft 155558sec 4: br0 inet brd scope global br0\ valid_lft forever preferred_lft forever 5: virbr0 inet brd scope global virbr0\ valid_lft forever preferred_lft forever

$ ip -o -f inet addr show | grep enp | awk '{print "Interface Name <" $2 ">"}'
Interface Name <enp7s0>

$ ip -o -f inet addr show | grep enp | awk '{print "IP and Mask <" $4 ">"}'
IP and Mask <>

$ ip -o -f inet addr show | grep enp | awk '{print "IP <" $4 ">"}' | sed -e 's|/.*|>|'
IP <>

$ ip -o -f inet addr show | grep enp | awk '{print "<" $4 ">"}' | sed -e 's|.*/|Mask <|'
Mask <24>

Here is my last attempt:

ip -o -f inet addr show | perl6 -e 'for ( lines ) -> $I { if ( $I ~~ /enp/ ) { say "Interface <" ~ .words[1] ~ ">\nIP<" ~ .words[3] ~ ">"; } }'
No such method 'words' for invocant of type 'Any'
  in block <unit> at -e line 1

Computers are like air conditioners.
They malfunction when you open windows

Reply via email to