I need to troubleshoot some issues on a client PC that I am unable to install additional software on and need some basic functionality of awk on windows xp & 7.

Basically, I need to strip the process ID from netstat so I can use it down the line in different parts of a script but have no knowledge of a native command that could do the equivalent of awk '{ print $4 }'. Are there any options for me? Obviously I'm quite new to windows cli.

  • would portable apps work? – Journeyman Geek Dec 27 '12 at 16:04
  • does the client pc already have perl installed? If not, try : stackoverflow.com/questions/98363/… – FreudianSlip Dec 27 '12 at 16:06
  • FreudianSlip, No perl. Journeyman, a portable app could probably work, but it creates more steps for the clients to do. I'm trying to set up something that they can copy/paste or 1 click run when the issue occurs next, having them locate the app or modify %PATH% or whatever else could work but might go over some of their heads. – Grambot Dec 27 '12 at 16:08

To get the Process ID (PID):

  • open cmd.exe and type in:
  • tasklist /fi "imagename eq netstat.exe" > D:\test.txt

That command will create a new text file with the process ID of netstat (if netstat is running).
The problem is, that this file will contain more than only the PID.

Maybe its enough for you to start with?

| improve this answer | |
  • Its a start, but I'm using netstat to locate the PID of a process making a remote connection via VPN. This process, I then need details from tasklist for more data but want to make it all happen in one step. – Grambot Dec 27 '12 at 16:18
  • The more I think about it, the more I realize I'm approaching this wrong. The details and data I need will be trivial at best to solving the problem and I got hooked in the loop of wanting more than I need: overcomplicating my issue. I'll mark you as correct as I can use your command with my process name substituted to get exactly what I need. – Grambot Dec 27 '12 at 16:19

For basic tasks like this, you can use for /f.

Instead of

command | awk '{ print $4 }'

try this:

for /f "tokens=4" %a in ('command') do echo %a

Note that you have to use %%a instead of %a in batch files.

| improve this answer | |

Sample netstat -n -o output:

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP        ESTABLISHED     4321
  TCP        ESTABLISHED     5432

To strip out the top 2 lines pipe the output to findstr /v "Active Proto".

To print only specific column data, for example the Local Address and PID, use:

for /f "tokens=2,5" %i in ('netstat -n -o ^| findstr /v "Active Proto"') do @echo Local Address = %i, PID = %j

If using in a batch file, you have to double the % signs, and can assign the values you want (%%i, %%j etc.) to variables.

| improve this answer | |

Elaborating on @Dennis's answer, here is a snippet I used to extract a version number out of a C header file, and use it to create a new file name.

My header file looks like

 ... stuff
 #define FIRMWARE 8
 .. stuff

The equivalent of UNIX

 f=`awk /FIRMWARE/ { print "image_v" $3 ".txt"; } project_config.`
 echo $f

in DOS is

 for /f "tokens=3" %%a in ('findstr FIRMWARE project_config.h') do (
   set f=image_v%%a.txt

 echo %f%
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.