简体   繁体   中英

How can I parse the top line of output of a previous command as input to another command?

In bash I would use awk and xargs for this, but I'm not sure how to do it on the windows command line.

Basically I am trying to kill the process listening on port 3000. I am currently doing it like this:

netstat -ano | findstr:3000
taskkill /PID <process id found from previous command> /F

Is there a way to automatically parse the previous command to chain these in one line so I don't have to keep manually typing the latest PID? Or if anyone knows a better way, that's appreciated too.

You will need to use a for loop to do this just like the following:

for /f "tokens=5" %A in ('netstat -ano ^| findstr /c:3000 ^| findstr /c:LISTENING') do taskkill /PID %A /F

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM