Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  TheCapn Dec 27 '12 at 16:08

4 Answers 4

up vote 2 down vote accepted

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?

share|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. –  TheCapn 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. –  TheCapn 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.

share|improve this answer

Sample netstat -n -o output:

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    127.0.0.1:12345        127.0.0.1:23456        ESTABLISHED     4321
  TCP    127.0.0.1:34567        127.0.0.1:45678        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.

share|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%
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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