• Re: Is there a native Windows command to pipe for a line count?

    From Marion@21:1/5 to Marion on Thu Mar 13 12:11:27 2025
    On Thu, 13 Mar 2025 11:58:30 -0000 (UTC), Marion wrote :


    My Windows question is whether or not there's a *better way* to get the number of lines output by any given shell command in Windows 10 or not?

    Duh. Nevermind... brain fart... I was in a rush... it was already there.

    adb shell pm list packages | find /c /v ""
    943 installed packages
    adb shell pm list packages -s | find /c /v ""
    391 system packages
    adb shell pm list packages -f -3 | find /c /v ""
    552 third-party packages

    /c display only a count of the lines containing the specified string.
    /v inverts the search
    "" matches an empty string

    This finds lines not containing the empty string, but since every line
    contains the empty string, this effectively counts all lines. I think.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marion@21:1/5 to All on Thu Mar 13 11:58:30 2025
    I haven't found a *native* method yet to get the app count from my Samsung phone, so I used Windows adb to spit out the installed user/sys packages.
    *list all installed packages in android adb shell*
    <https://gist.github.com/davidnunez/1404789?permalink_comment_id=3278028>

    Then I needed a line count of the number of lines, which I could only do by counting the lines in the output file; but I wonder if there's a native way
    to get a line count directly out of a shell command?

    adb shell pm list packages -f > packages.txt
    find /c /v "" packages.txt
    This reports 943 lines in "packages.txt".

    To find out how many are 3rd-party (not system) packages:
    adb shell pm list packages -f -3 > 3rdparty.txt
    find /c /v "" 3rdparty.txt
    This reports 552 lines in "3rdparty.txt".

    To find out how many system packages are installed:
    adb shell pm list packages -s > system.txt
    find /c /v "" system.txt
    This reports 391 lines in "system.txt".

    BTW, it's interesting that Muntashirakon listed slightly different numbers.
    <https://github.com/MuntashirAkon/AppManager>
    <https://muntashirakon.github.io/AppManager/en/>
    <https://muntashirakon.github.io/AppManager/vi/>

    My Windows question is whether or not there's a *better way* to get the
    number of lines output by any given shell command in Windows 10 or not?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul@21:1/5 to Marion on Thu Mar 13 10:57:55 2025
    On Thu, 3/13/2025 8:11 AM, Marion wrote:
    On Thu, 13 Mar 2025 11:58:30 -0000 (UTC), Marion wrote :


    My Windows question is whether or not there's a *better way* to get the
    number of lines output by any given shell command in Windows 10 or not?

    Duh. Nevermind... brain fart... I was in a rush... it was already there.

    adb shell pm list packages | find /c /v ""
         943 installed packages
    adb shell pm list packages -s | find /c /v ""
         391 system packages
    adb shell pm list packages -f -3 | find /c /v ""
         552 third-party packages

    /c display only a count of the lines containing the specified string.
    /v inverts the search
    "" matches an empty string

    This finds lines not containing the empty string, but since every line contains the empty string, this effectively counts all lines. I think.

    Write-Host -NoNewline "Number of Apps = " ; cat d:\test.txt | Measure-Object -Line | Select-Object -ExpandProperty Lines
    Number of Apps = 15679


    Substitute my "cat" call with your text generating stanza.

    Paul

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marion@21:1/5 to Paul on Sun Mar 16 06:46:20 2025
    On Thu, 13 Mar 2025 10:57:55 -0400, Paul wrote :


    This finds lines not containing the empty string, but since every line
    contains the empty string, this effectively counts all lines. I think.

    Write-Host -NoNewline "Number of Apps = " ; cat d:\test.txt | Measure-Object -Line | Select-Object -ExpandProperty Lines
    Number of Apps = 15679


    Substitute my "cat" call with your text generating stanza.

    Thanks. I hate power shell. But I appreciate that it answers the question!
    I wasn't aware that "cat" (concatonate) is a Windows' native command.
    Lesson learned.

    Write-Host -NoNewline "Number of Apps = " ; cat test.txt | Measure-Object -Line | Select-Object -ExpandProperty Lines

    This output:
    Number of Apps = 916

    BTW, I explained the command line options incorrectly in my prior post.
    This is corrected.
    find: A command-line utility used to search for text strings.
    /c: Count the number of lines that contain the specified string.
    /v: Display all lines not containing the specified string.
    "": This is an empty string.

    Therefore, find /c /v "" searches for lines that do not contain an empty string, and then counts those lines. Since every line contains an empty
    string, /v "" will match no lines. In effect, the /v option is negated by
    the empty search string.

    However, when combined with /c, it does something very specific: it counts
    all lines of input.

    In essence, the command | find /c /v "" counts the number of lines received through the pipe (|).

    Here's a breakdown of why this works:
    a. Every line contains an empty string:
    An empty string is considered to be present in any string,
    including a blank line.
    b. /v "" inverts the search:
    It looks for lines that don't contain an empty string.
    Since every line has an empty string, no lines match /v "" alone.
    c. However, /c overrides the normal output:
    Instead of displaying the matching lines (which would be none),
    it counts them.

    The trick is that find /c when used with /v "" will count all input lines.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stan Brown@21:1/5 to Marion on Sun Mar 16 14:38:53 2025
    On Sun, 16 Mar 2025 06:46:20 -0000 (UTC), Marion wrote:
    I wasn't aware that "cat" (concatonate) is a Windows' native command.
    Lesson learned.

    I fear you learned the wrong lesson. When I type "cat /?" I get

    'cat' is not recognized as an internal or external command,
    operable program or batch file.


    --
    Stan Brown, Tehachapi, California, USA https://BrownMath.com/
    Shikata ga nai...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul@21:1/5 to Stan Brown on Sun Mar 16 23:16:55 2025
    On Sun, 3/16/2025 5:38 PM, Stan Brown wrote:
    On Sun, 16 Mar 2025 06:46:20 -0000 (UTC), Marion wrote:
    I wasn't aware that "cat" (concatonate) is a Windows' native command.
    Lesson learned.

    I fear you learned the wrong lesson. When I type "cat /?" I get

    'cat' is not recognized as an internal or external command,
    operable program or batch file.



    You're typing it in the wrong window.

    It's a powershell cmdlet.

    My line of stuff was preceded by this:



    If you entered "cat" into a Command Prompt,
    no, it isn't there.

    If you want a "cat.exe" , there should be
    one for you, if you want it. But that
    wasn't my intention.

    One of the reasons I wouldn't give you this (scroll down a bit):

    https://gnuwin32.sourceforge.net/packages/coreutils.htm

    is because Windows 11 does not handle dyndlls properly any
    more, which makes the setup of one of those utilities,
    an extra pain in the ass. It would be faster for me to
    write my own cat program, than do that :-) While I could
    use DependencyWalker to collect dependency info and
    develop a guess at the DLLs needed from SourceForge,
    nobody wants to do that. They want software that works.
    All of those executables could have been statically compiled
    and then they would be ready to go.

    Paul

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marion@21:1/5 to Paul on Mon Mar 17 04:22:31 2025
    On Sun, 16 Mar 2025 23:16:55 -0400, Paul wrote :


    If you want a "cat.exe" , there should be
    one for you, if you want it. But that
    wasn't my intention.

    It turns out cat doesn't exist in PowerShell. But it does. Kind of.
    It's an alias. See below for details.

    All of us who came from the old school (i.e., well before the PC existed,
    we were on various operating systems, one of which morphed into Linux), we
    used "cat" all the time, so it was a surprise to me it's in power shell.

    Thanks for letting me know. It will come in handy (if only there was awk,
    sed, grep, etc.) but we all tried cygwin type stuff before WSL came about.

    Then we tried WSL, and hated it (at least I hated it when I tried it).
    BTW, here's an explanation of the wonderful PowerShell command you listed.

    Write-Host -NoNewline "Number of Apps = " ; cat test.txt | Measure-Object -Line | Select-Object -ExpandProperty Lines

    Write-Host -NoNewline "Number of Apps = ":
    Write-Host is used to display text on the console.

    -NoNewline prevents the cursor from moving to the next line after the
    text is displayed. This allows the subsequent output to appear
    on the same line.

    "Number of Apps = " is the string that will be displayed.

    cat test.txt:
    cat is an alias for Get-Content.
    It reads the contents of the file named "test.txt".
    Each line of the file is treated as a separate object in the pipeline.

    Measure-Object -Line:
    Measure-Object with the -Line parameter counts the number of lines in the input.

    Select-Object -ExpandProperty Lines:
    Select-Object is used to select specific properties of an object.
    -ExpandProperty Lines extracts the value of the "Lines" property from the
    Measure-Object output and sends only that numerical value to the console.

    Who knew all that existed? Not me. I hate powershell.
    But I have to admit, it gives us the alias "cat" so that's a good thing.

    Now if only there was an alias for "grep" (yes, I know, there is findstr).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marion@21:1/5 to Marion on Mon Mar 17 04:30:35 2025
    On Mon, 17 Mar 2025 04:22:31 -0000 (UTC), Marion wrote :


    cat test.txt:
    cat is an alias for Get-Content.
    It reads the contents of the file named "test.txt".
    Each line of the file is treated as a separate object in the pipeline.

    I was hoping, beyond hope, that this would work, but it failed.

    cat foo.txt | find /c /v ""
    FIND: Parameter format not correct

    I had to do this, instead:
    (Get-Content foo.txt | Where-Object { $_.Trim() -ne "" }).Count

    Yuck. Did I mention how much I hate PowerShell yet?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)