From Newsgroup: alt.os.linux
Lawrence DoOliveiro wrote:
It's too bad the MS Rust CoreUtils don't have awk & sed though...
perl >> awk
sed ... I suppose it has its uses ...
Hi Lawrence,
In interpreting your sagacious comment above, I have to openly admit that
I'm not a coder, in that I've programmed extensively, but in IBM360
Assembly & JCL, COBOL, Fortan IV, PL/1, etc., which, as you can tell, dates
me back to the 1960's and 1970s' as the last time I did serious coding.
Perl, by way of contrast, dating only to 1987, is a newcomer, to me.
So I've never actually used Perl.
However, if someone invests the energy to make a suggestion on Usenet,
I likewise invest energy in at least attempting to UNDERSTNAD what they are trying to impart.
Given your "perl >> awk" suggestion above, noting that every post to Usenet should, by design, kick the ball forward, I tested out that premise below.
Looking up the advantages of Perl, apparently Perl users traditionally view sed as limited because Perl can do everything sed can do (via regular expression substitutions like s/find/replace/), but with the added power
of a full programming language.
So let's test it out...
perl
'perl' is not recognized as an internal or external command,
operable program or batch file.
winget install StrawberryPerl
Found Strawberry Perl [StrawberryPerl.StrawberryPerl] Version 5.42.2.1
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to,
third-party packages. Downloading
https://github.com/StrawberryPerl/Perl-Dist-Strawberry/releases/download/SP_54221_64bit/strawberry-perl-5.42.2.1-64bit.msi
200 MB / 200 MB
Successfully verified installer hash
Starting package install...
Successfully verified installer hash
Starting package install...
Successfully installed
where perl
C:\Strawberry\perl\bin\perl.exe
Note that this is an ungodly dumb location for the default to be.
Looking up why such an idiotic location is the default, ironically
indicates that the Windows perl is incapable of handling spaces
in the path correctly (e.g., "Program<space>Files"). </Irony>
Still, in Windows cmd.exe, single quotes (') do NOT protect variables
the way they do in Linux, so we still have the same horrors to overcome.
WSL awk:
adb shell dumpsys battery | tr -d '\r' | awk "/level:/ {print \"Battery Level:\", \$2 \"%\"}"
Battery Level: 92%
Strawberry Perl:
We have to bypass cmd.exe's habit of mishandling single quotes.
adb shell dumpsys battery | perl -ne "s/\r//; if (/level:\s*(\d+)/) { print 'Battery Level: $1%\n' }"
Battery Level: $1%\n
We also have to bypass cmd.exe's swallowing backslashes/newlines (\n)
and we have to also protect variables from cmd.exe expansion.
adb shell dumpsys battery | perl -ne "s/\r//; if (/level:\s*(\d+)/) { print 'Battery Level: ' . $1 . '%%\n'; }"
Battery Level: 92%%\n
Resulting in a working example using Perl flags (-nl) and qq{} interpolation,
but the percent sign must be doubled to avoid Perl misinterpreting it as a hash.
adb shell dumpsys battery | perl -nl -e "if (/level:\s*(\d+)/) { print qq{Battery Level: $1%%}; }"
Battery Level: 92%%
So we use Perl's hex ASCII code (\x25) to get rid of the duplicated
percent sign to ensure a clean terminal exit.
adb shell dumpsys battery | perl -nl -e "if (/level:\s*(\d+)/) { print qq{Battery Level: $1\x25}; }"
Battery Level: 92%
WSL awk:
adb shell ip addr show wlan0 | awk '/inet / {print "IP Address:", $2}'
IP Address: 192.168.1.4/24
Strawberry Perl:
We have the same problem of mishandling of quotes and newlines.
adb shell ip addr show wlan0 | perl -ne "s/\r//; if (/inet\s+(\S+)/) { print 'IP Address: $1\n' }"
IP Address: $1\n
adb shell ip addr show wlan0 | perl -nl -e "if (/inet\s+(\S+)/) { print qq{IP Address: $1}; }"
IP Address: 192.168.1.4/24
This is apparently due to quirks in how Windows cmd.exe handles syntax compared to how Linux bash handles that same syntax.
1. Single quotes work fine in Linux, but not in Windows
(so we have to use double quotes instead)
2. Unescaped dollar signs work fine in Linux, but not in Windows
(so we have to escape the dollar signs instead)
3. Linux adb outputs LF (\n) while Windows adb outputs CRLF (\r\n)
(so we have to pipe it to a translate to clean up line endings)
But once we figure those issues out, perl works as well as awk.
adb shell getprop | tr -d '\r' | awk -F': ' '/ro\.product\.model/ {gsub(/[\[\]]/, "", $2); print "Device Model:", $2}'
Device Model: SM-A326U
adb shell getprop | perl -nl -e "if (/\[ro\.product\.model\]:\s*\[(.*?)\]/) { print qq{Device Model: $1}; }"
Device Model: SM-A326U
The question, I guess, is which is the least worst way of doing it?
--
Every fixed example is someone paying forward a hard-learned lesson.
--- Synchronet 3.22a-Linux NewsLink 1.2