Re: awk?
On 04/13/2018 04:13 AM, Fernando Santagata wrote: "There's More Than One Way To Do It" The definition of Perl! :-)
Re: awk?
On Friday, April 13, Fernando Santagata wrote: Hi, Since "There's More Than One Way To Do It", one can look for the value, instead of the separator: $ echo "total kB 1804482980 112" |perl6 -n -e 'say .comb(/\d+/)[1]' 2980 On Fri, Apr 13, 2018 at 12:31 PM, Shlomi Fish <shlo...@shlomifish.org> wrote: Hi Todd, On Fri, 13 Apr 2018 03:00:22 -0700 ToddAndMargo <toddandma...@zoho.com> wrote: echo "total kB 1804482980 112" | awk '{print $4}') shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | perl6 -n -e 'say .split(/\s+/)[3]' 2980 shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | awk '{print $4}' 2980 See https://docs.perl6.org/language/5to6-nutshell#-a and http://perldoc.perl.org/perlrun.html . Fernando Santagata On 04/13/2018 05:20 AM, Brian Duggan wrote: One could also use .words -- $ echo "total kB 1804482980 112" |perl6 -n -e 'say .words[3]' 2980 Brian That almost exactly matches awk and I don't have to wonder about what each value is made up of. I love it. Thank you! -T -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: awk?
On Fri, Apr 13, 2018 at 12:31 PM, Shlomi Fish <shlo...@shlomifish.org <mailto:shlo...@shlomifish.org>> wrote: Hi Todd, On Fri, 13 Apr 2018 03:00:22 -0700 ToddAndMargo <toddandma...@zoho.com <mailto:toddandma...@zoho.com>> wrote: > echo "total kB 1804482980 112" | awk '{print $4}') shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | perl6 -n -e 'say .split(/\s+/)[3]' 2980 shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | awk '{print $4}' 2980 See https://docs.perl6.org/language/5to6-nutshell#-a <https://docs.perl6.org/language/5to6-nutshell#-a> and http://perldoc.perl.org/perlrun.html <http://perldoc.perl.org/perlrun.html> . On 04/13/2018 04:13 AM, Fernando Santagata wrote: Hi, Since "There's More Than One Way To Do It", one can look for the value, instead of the separator: $ echo "total kB 180448 2980 112" |perl6 -n -e 'say .comb(/\d+/)[1]' 2980 So the second d+ is [1]. Fascinating. Thank you!
Re: awk?
On 04/13/2018 03:31 AM, Shlomi Fish wrote: Hi Todd, On Fri, 13 Apr 2018 03:00:22 -0700 ToddAndMargo <toddandma...@zoho.com> wrote: echo "total kB 1804482980 112" | awk '{print $4}') shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | perl6 -n -e 'say .split(/\s+/)[3]' 2980 shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | awk '{print $4}' 2980 See https://docs.perl6.org/language/5to6-nutshell#-a and http://perldoc.perl.org/perlrun.html . Aw bugger! I forgot about split! And I use it all the time. Thank you! -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: awk?
One could also use .words -- $ echo "total kB 1804482980 112" |perl6 -n -e 'say .words[3]' 2980 Brian On Friday, April 13, Fernando Santagata wrote: > Hi, > > Since "There's More Than One Way To Do It", one can look for the value, > instead of the separator: > > $ echo "total kB 1804482980 112" |perl6 -n -e 'say > .comb(/\d+/)[1]' > 2980 > > > On Fri, Apr 13, 2018 at 12:31 PM, Shlomi Fish <shlo...@shlomifish.org> > wrote: > > > Hi Todd, > > > > On Fri, 13 Apr 2018 03:00:22 -0700 > > ToddAndMargo <toddandma...@zoho.com> wrote: > > > > > echo "total kB 1804482980 112" | awk '{print $4}') > > > > shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | > > perl6 > > -n -e 'say .split(/\s+/)[3]' > > > > 2980 > > > > shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | > > awk > > '{print $4}' > > > > 2980 > > > > See https://docs.perl6.org/language/5to6-nutshell#-a and > > http://perldoc.perl.org/perlrun.html . > > > > -- > > - > > Shlomi Fish http://www.shlomifish.org/ > > What does “Zionism” mean? - http://shlom.in/def-zionism > > > > Gödel’s Incompleteness Theorem is about to be replaced by the > > [Clarissa] Darling “Like, Totally!” Completeness Theorem. > > — http://www.shlomifish.org/humour/bits/facts/Clarissa/ > > > > Please reply to list if it's a mailing list post - http://shlom.in/reply . > > > > > > -- > Fernando Santagata
Re: awk?
Hi, Since "There's More Than One Way To Do It", one can look for the value, instead of the separator: $ echo "total kB 1804482980 112" |perl6 -n -e 'say .comb(/\d+/)[1]' 2980 On Fri, Apr 13, 2018 at 12:31 PM, Shlomi Fish <shlo...@shlomifish.org> wrote: > Hi Todd, > > On Fri, 13 Apr 2018 03:00:22 -0700 > ToddAndMargo <toddandma...@zoho.com> wrote: > > > echo "total kB 1804482980 112" | awk '{print $4}') > > shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | > perl6 > -n -e 'say .split(/\s+/)[3]' > > 2980 > > shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | > awk > '{print $4}' > > 2980 > > See https://docs.perl6.org/language/5to6-nutshell#-a and > http://perldoc.perl.org/perlrun.html . > > -- > - > Shlomi Fish http://www.shlomifish.org/ > What does “Zionism” mean? - http://shlom.in/def-zionism > > Gödel’s Incompleteness Theorem is about to be replaced by the > [Clarissa] Darling “Like, Totally!” Completeness Theorem. > — http://www.shlomifish.org/humour/bits/facts/Clarissa/ > > Please reply to list if it's a mailing list post - http://shlom.in/reply . > -- Fernando Santagata
Fw: awk?
Begin forwarded message: Date: Fri, 13 Apr 2018 13:31:12 +0300 From: Shlomi Fish <shlo...@shlomifish.org> To: perl6-us...@perl.org Subject: Re: awk? Hi Todd, On Fri, 13 Apr 2018 03:00:22 -0700 ToddAndMargo <toddandma...@zoho.com> wrote: > echo "total kB 180448 2980 112" | awk '{print $4}') shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | perl6 -n -e 'say .split(/\s+/)[3]' 2980 shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | awk '{print $4}' 2980 See https://docs.perl6.org/language/5to6-nutshell#-a and http://perldoc.perl.org/perlrun.html .
Re: awk?
Hi Todd, On Fri, 13 Apr 2018 03:00:22 -0700 ToddAndMargo <toddandma...@zoho.com> wrote: > echo "total kB 1804482980 112" | awk '{print $4}') shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | perl6 -n -e 'say .split(/\s+/)[3]' 2980 shlomif[Perl6]:$trunk$ echo "total kB 1804482980 112" | awk '{print $4}' 2980 See https://docs.perl6.org/language/5to6-nutshell#-a and http://perldoc.perl.org/perlrun.html . -- - Shlomi Fish http://www.shlomifish.org/ What does “Zionism” mean? - http://shlom.in/def-zionism Gödel’s Incompleteness Theorem is about to be replaced by the [Clarissa] Darling “Like, Totally!” Completeness Theorem. — http://www.shlomifish.org/humour/bits/facts/Clarissa/ Please reply to list if it's a mailing list post - http://shlom.in/reply .
awk?
Hi All, How to I translate this to Perl 6? I am looking to be able to dig out the fourth (or any) data point. #!bin/bash $x=$(echo "total kB 1804482980 112" | awk '{print $4}') echo $x 2980 Many thanks, -T
Re: Need awk print sub
On 08/04/2017 03:42 PM, Brandon Allbery wrote: On Fri, Aug 4, 2017 at 6:28 PM, ToddAndMargo> wrote: 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 at -e line 1 Somewhere above I mentioned that the default for .words was $_. Not $I, which you have chosen to use. So you'll need to specify the explicit invocant: ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /enp/ ) { say "Interface <" ~ $I.words[0] ~ ">\nIP<" ~ $I.words[3] ~ ">"; } }' If I swap out your match for a prefix that exists on my system: pyanfar Z$ ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /wlan/ ) { say "Interface <" ~ $I.words[1] ~ ">\nIP<" ~ $I.words[3].split("/")[0] ~ ">"; } }' Interface IP<10.8.201.162> (For this one, I also removed the CIDR mask from the end.) Yippee! $ ip -o -f inet addr show | perl6 -e 'for lines() -> $Str { if ( $Str ~~ /enp/ ) { say "\n$Str\n\nInterface <" ~ $Str.words[1] ~ ">\nIP<" ~ $Str.words[3].split("/")[0] ~ ">\n" ~ "Short Mask <" ~ $Str.words[3].split("/")[1] ~ ">\n"; } }' 3: enp7s0inet 192.168.250.135/24 brd 192.168.250.255 scope global dynamic enp7s0\ valid_lft 152704sec preferred_lft 152704sec Interface IP<192.168.250.135> Short Mask <24> My big misunderstanding was not putting the variable name in front of the .words. Thank you!!! As an exercise, I redid the thing as a program. You guys may want my RunNoShell subroutine Also at vpaste (no truncated lines): http://vpaste.net/UFtOJ #!/usr/bin/env perl6 my $ipStr; my $ipRtnCode; my $ipInterface; my $ipIP; my $ipShortMask; my $ipLongMask; sub RunNoShell ( $RunString) { #`{ run a system command without a shell. Enter the command as a sting. Use quotes around items that are meant to be grouped together ("Program Files", etc.) Results are returned as a dual pair: a string with the results of the command, and the return code (note: bash 0=success) } # place each value into a cell in an array. Keep quoted values together my @RunArray = $RunString ~~ m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /; # shells remove the quote, so you have to here as well for @RunArray.kv -> $index, $value { @RunArray[$index] ~~ s:g/\"//; }; # for @RunArray.kv -> $index, $value { say "\$RunArray[$index] = <$value>"; }; print "\n"; my $proc = run( @RunArray, :out ); my $ReturnStr = $proc.out.slurp-rest; my $RtnCode = $proc.status; return ( $ReturnStr, $RtnCode ); } ( $ipStr, $ipRtnCode ) = RunNoShell ( "ip -o -f inet addr show" ); for ( $ipStr.lines ) -> $Line { if ( $Line ~~ /enp/ ) { $ipStr = $Line; last; } } $ipInterface = $ipStr.words[1]; $ipIP= $ipStr.words[3].split("/")[0]; $ipShortMask = $ipStr.words[3].split("/")[1]; $ipLongMask = $ipStr.words[5]; say " Interface = <$ipInterface>"; say " IP = <$ipIP>"; say " Short Mask = <$ipShortMask>"; say " Long Mask = <$ipLongMask>\n"; $ ./GetNetwork.pl6 Interface = IP = <192.168.250.135> Short Mask = <24> Long Mask = <192.168.250.255>
Re: Need awk print sub
On Fri, Aug 4, 2017 at 6:28 PM, ToddAndMargowrote: > 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 at -e line 1 > Somewhere above I mentioned that the default for .words was $_. Not $I, which you have chosen to use. So you'll need to specify the explicit invocant: ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /enp/ ) { say "Interface <" ~ $I.words[0] ~ ">\nIP<" ~ $I.words[3] ~ ">"; } }' If I swap out your match for a prefix that exists on my system: pyanfar Z$ ip -o -f inet addr show | perl6 -e 'for lines() -> $I { if ( $I ~~ /wlan/ ) { say "Interface <" ~ $I.words[1] ~ ">\nIP<" ~ $I.words[3].split("/")[0] ~ ">"; } }' Interface IP<10.8.201.162> (For this one, I also removed the CIDR mask from the end.) -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: Need awk print sub
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}' b $ 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? Certainly! Warning: the following may cause brain damage. I am looking to extract interface name, IP, and short mask. $ ip -o -f inet addr show 1: loinet 127.0.0.1/8 scope host lo\ valid_lft forever preferred_lft forever 3: enp7s0inet 192.168.250.135/24 brd 192.168.250.255 scope global dynamic enp7s0\ valid_lft 18sec preferred_lft 18sec 4: br0inet 192.168.255.10/24 brd 192.168.255.255 scope global br0\ valid_lft forever preferred_lft forever 5: virbr0inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0\ valid_lft forever preferred_lft forever $ ip -o -f inet addr show | grep enp | awk '{print "Interface Name <" $2 ">"}' Interface Name $ ip -o -f inet addr show | grep enp | awk '{print "IP and Mask <" $4 ">"}' IP and Mask <192.168.250.135/24> $ ip -o -f inet addr show | grep enp | awk '{print "IP <" $4 ">"}' | sed -e 's|/.*|>|' IP <192.168.250.135> $ 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 at -e line 1 -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: Need awk print sub
On 08/04/2017 02:31 PM, Elizabeth Mattijsen wrote: On 4 Aug 2017, at 22:53, ToddAndMargowrote: On 08/04/2017 01:26 PM, Elizabeth Mattijsen wrote: echo "a b c d" | perl6 -e 'say words[1]’ Tears. :'( $ echo "a b c d" | perl6 -e 'say words[1]' ===SORRY!=== Error while compiling -e Calling words() will never work with any of these multi signatures: ($what, $limit = Inf, *%named) at -e:1 --> say ⏏words[1] That means you’re using a pre-2017.07 rakudo Liz Hi Liz, $ perl6 -v This is Rakudo version 2017.03 built on MoarVM version 2017.03 implementing Perl 6.c. Have you every heard the term "Kaisen"? It is a Japanese word and comes from Dr. Demming's teachings (quality circles, etc.). It means "Constant Improvement". Well, now, I am stuck with using Scientific Linux 7.3 (Red Hat Enterprise Linux 7.3 clone), also know as "Old-Out-Of-Date". Red Hat's philosophy is to freeze known revisions in place -- bugs and all -- to insure "stability". RHEL is an anti-Kaisen OS. Since RHEL doesn't work on C236 chipset based motherboards: https://bugzilla.redhat.com/show_bug.cgi?id=1353423 I have been building Fedora servers instead. And I have to tell you, the frustration factor with running an Anti-Kaisen OS versus a Kaisen OS are about 10 to 1. I will dump my SL box as soon I can afford it. I have HAD IT with RHEL. RHEL is NOT more stable than Fedora. The long and short of it is, I AM LUCKY TO EVEN HAVE PERL 6. I can ask EPEL (Extra Packages for Enterprise Linux) to update to 2017.07, but they will politely tell me to go to ... Is there any way to work around the issue? -T -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: Need awk print sub
> On 4 Aug 2017, at 22:53, ToddAndMargowrote: > > On 08/04/2017 01:26 PM, Elizabeth Mattijsen wrote: >> echo "a b c d" | perl6 -e 'say words[1]’ > > > Tears. :'( > > $ echo "a b c d" | perl6 -e 'say words[1]' > > ===SORRY!=== Error while compiling -e > Calling words() will never work with any of these multi signatures: >($what, $limit = Inf, *%named) > at -e:1 > --> say ⏏words[1] That means you’re using a pre-2017.07 rakudo Liz
Re: Need awk print sub
(back on the desktop for the moment...) On Fri, Aug 4, 2017 at 5:04 PM, ToddAndMargo <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}' >> b >> > > $ 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? -- brandon s allbery kf8nh sine nomine associates allber...@gmail.com ballb...@sinenomine.net unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net
Re: Need awk print sub
On 08/04/2017 01:00 PM, ToddAndMargo wrote: Hi All, How do I do this with a perl one liner? $ echo "a b c d" | awk '{print $2}' b Many thanks, -T And it is getting weirder: $ 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 -- Yesterday it worked. Today it is not working. Windows is like that.
Re: Need awk print sub
On 08/04/2017 01:53 PM, ToddAndMargo wrote: $ echo "a b c d" | perl6 -e 'say words[1]' And with the . before words $ echo "a b c d" | perl6 -e 'say .words[1]' No such method 'words' for invocant of type 'Any' in block at -e line 1
Re: Need awk print sub
>> Hi All, >> >> How do I do this with a perl one liner? >> >> $ echo "a b c d" | awk '{print $2}' >> b >> >> Many thanks, >> -T On 08/04/2017 01:31 PM, Patrick R. Michaud wrote: How about... $ echo "a b c d" | ./perl6 -n -e '.words[1].say' b Pm On Fri, Aug 04, 2017 at 01:00:52PM -0700, ToddAndMargo wrote: I can't win. :'( $ echo "a b c d" | perl6 -e 'say lines ~ "\n" ~ .words[2];' No such method 'words' for invocant of type 'Any' in block at -e line 1 -- ~~ Computers are like air conditioners. They malfunction when you open windows ~~
Re: Need awk print sub
> On 4 Aug 2017, at 22:00, ToddAndMargo <toddandma...@zoho.com> wrote: > > Hi All, > > How do I do this with a perl one liner? > > $ echo "a b c d" | awk '{print $2}' > b echo "a b c d" | perl6 -e 'say words[1]’ Note array indices in Perl 6 are 0 based.
Need awk print sub
Hi All, How do I do this with a perl one liner? $ echo "a b c d" | awk '{print $2}' b Many thanks, -T