locked
How to get command result to a variable RRS feed

  • Question

  • Hello,

    I have the following commands to get the number of lines that contains "here is the line". However the variable linecount is always null for me.

    Any suggestion?

    Thanks!

    set linecount=(typet test.txt | find /c "here is the line')

    echo linecount=%linecount

    Monday, May 12, 2014 5:42 PM

Answers

  • Try this...

     @echo off
      for /f %%A in ('find /c "here is the line" ^< test') do set linecout=%%A
      echo linecount=%linecount%

    Note the closing percent sign on the environment variable's name in the ECHO statement.


    Tom Lavedas


    • Edited by Tom Lavedas Monday, May 12, 2014 6:22 PM took out an unnecessary bit of the code
    • Marked as answer by Bill_Stewart Wednesday, June 4, 2014 3:39 PM
    Monday, May 12, 2014 6:21 PM

All replies

  • You've got a typo in your code ("typet" instead of "type").  Was that just on the forum post, or is that error also in the batch file that you've run?
    Monday, May 12, 2014 6:04 PM
  • What's your purpose/goal? For the LINECOUNT environment variable to contain the line number of the matching string?

    This is very short in PowerShell:


    $lines = get-content "test.txt" |
      select-string "here is the line" |
      foreach-object { $_.LineNumber }

    In this case, the variable $lines will contain 0 or more numbers where each number is the line number in test.txt that contained the matching string. If you want to duplicate find /c (get number of lines in the file that contain the string, not a list of line numbers), you can write this:


    $lines = (get-content "test.txt" |
      select-string "here is the line" |
      foreach-object { $_.LineNumber } |
      measure-object).Count
    


    -- Bill Stewart [Bill_Stewart]

    Monday, May 12, 2014 6:08 PM
  • Or if it really needs to be in batchscript format, you can use :

    for /f "delims=" %%a in ('find /c "here is the line" test.txt') do @set linecount=%%a
    echo linecount=%linecount%

    Monday, May 12, 2014 6:19 PM
  • Try this...

     @echo off
      for /f %%A in ('find /c "here is the line" ^< test') do set linecout=%%A
      echo linecount=%linecount%

    Note the closing percent sign on the environment variable's name in the ECHO statement.


    Tom Lavedas


    • Edited by Tom Lavedas Monday, May 12, 2014 6:22 PM took out an unnecessary bit of the code
    • Marked as answer by Bill_Stewart Wednesday, June 4, 2014 3:39 PM
    Monday, May 12, 2014 6:21 PM
  • The results have more than I need:

    ----------- test.txt: 1

    Monday, May 12, 2014 8:15 PM
  • Try my PowerShell code. Easier to read.


    -- Bill Stewart [Bill_Stewart]

    Monday, May 12, 2014 8:18 PM
  • linecount=----------

    Monday, May 12, 2014 8:18 PM
  • Is there some reason you can't use PowerShell?

    -- Bill Stewart [Bill_Stewart]

    Monday, May 12, 2014 8:26 PM
  • In cmd.exe shell script (batch file):


    for /f "delims=" %%c in ('type "test.txt" ^| find /c "here is the line"') do set LINECOUNT=%%c
    echo %LINECOUNT%
    

    But as noted, the PowerShell code is a lot easier to read and understand.


    -- Bill Stewart [Bill_Stewart]

    Monday, May 12, 2014 9:01 PM
  • If you use redirection (the ^< shown in my previous post) instead of adding the file's name to the FIND statement in Mr. Langmead's version, the extra text is omitted.  That is, ...

      for /f "delims=" %%a in ('find /c "here is the line" ^< test.txt') do @set linecount=%%a
      echo linecount=%linecount%

    instead of ...

      for /f "delims=" %%a in ('find /c "here is the line" test.txt') do @set linecount=%%a
      echo linecount=%linecount%

    Also, note that the first version doesn't need the "delims=" part, though it won't change anything if its there or not.

    For more information on what's happening in this FOR statement, try typing FOR/? at the command prompt.  The FOR is an essential part of making the command line more useful, though this is an old technology, as Bill has said.


    Tom Lavedas

    Tuesday, May 13, 2014 1:51 PM