1

I need to extract a portion of text from a text file log.txt, where the contents of the file are as follows.

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
services                                    0  Disc                        
console                                     1  Conn                        
                   input1                   3  Disc                        
 rdp-tcp#0         input2                   4  Active  rdpwd               
                   input3                   6  Disc                        
 rdp-tcp                                 65536  Listen                      

Specifically, I need the SESSION ID of a specific username. Need help in extracting the same. Any advice or help will be much appreciated.

Thanks.

8
  • So you want to extract all the usernames and IDs?
    – uSlackr
    Nov 12, 2015 at 14:53
  • and can you humor us with what the command was that generated this response?
    – uSlackr
    Nov 12, 2015 at 14:53
  • No, just one user session, say for input1 I need the SESSION ID. And, I used qwinsta to get that response. Nov 12, 2015 at 15:18
  • So you want to be able to specify a username and get their session?
    – uSlackr
    Nov 12, 2015 at 15:36
  • If you know a specific username you want to get the ID for, you can filter the output by invoking qwinsta with the username as a parameter.
    – Ben N
    Nov 12, 2015 at 19:32

2 Answers 2

1

Next script shows possible approach using extract part of a variable (substring).

@ECHO OFF >NUL
SETLOCAL EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%G in ("D:\test\QuerySessionLog.txt") do (
  set "logline=%%~G"
  set "_SESSIONNAME=!logline:~0,18!"
  set "_USERNAME=!logline:~18,18!"
  set "_ID=!logline:~36,11!"
  set "_STATE=!logline:~47,8!"
  set "_TYPE=!logline:~55,7!"
  set "_DEVICE=!logline:~62!"

  echo !logline!
  for /F "tokens=1,* delims==" %%g in ('set _') do (
    set "trimentry=%%h"
    if "!trimentry:~0,1!"==">" (
      call :TRIM !trimentry:~1!
    ) else (
      call :TRIM !trimentry!
    )
    echo %%g=[!trimresult!] ^(raw=[%%h]^) 
  )
  echo( 
) 
:endlocal
ENDLOCAL
goto :eof

:TRIM
  SET "trimresult=%*"
GOTO :EOF

Please note that I'm absolutely not sure about rightness of column widths as another example provided in query session command reference shows another format.
However, column widths could be computable from the header line (but this would exceed current question topic).

1
  • Thanks a lot Josef, that was exactly what I wanted. Thanks to everyone else who tried. Nov 13, 2015 at 15:32
0

That looks like fixed-width data, so let's use GNU awk: user "input2" has id "4"

$ gawk -v FIELDWIDTHS="19 25 3 99" -v user=input2 '$2 ~ "^"user {print $3}' log.txt
4  
2
  • Oops, didn't notice this was for windows. Good luck. Nov 12, 2015 at 15:57
  • Thanks for the effort, Yes I need it for windows. Nov 12, 2015 at 16:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .