• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2056
  • Last Modified:

using findstr to output

The other day I used a command that worked fine, but its output required me to do some hands-on work in Excel to get exactly the output I wanted to copy/paste into an email.

Here was the command (sample IP)...

ping -a | findstr /i "Pinging" >> .\hostnames.txt

I am looking for a way to grep or findstr only the hostname. Do you know if I can do this simply using the old CMD tools?

John Darby
John Darby
  • 2
  • 2
2 Solutions
could try the following via batch or command line:

ping -a| find /i "pinging" > %temp%\pingtest.txt
for /f "tokens=2" %a in (%temp%\pingtest.txt) do echo %a >> .\hostnames.txt


you could try:

ping -a| for /f "tokens=1,2" %a in ('find /i "Pinging"') do echo %b >> .\hostnames.txt

note1 -  a double quotation mark followed by a single quotation mark follows the word Pinging.
note2 - if you plan to execute either suggestion above via a batch file you will need to add an extra % in front of the 'for' variables.

ex.  for /f "tokens=2" %%a...  etc.
you could also directly pipe the command line output to the clipboard:

ping -a| for /f "tokens=1,2" %a in ('find "Pinging"') do echo %b| "clip"

you could then create a new e-mail message and CTRL-V or SHIFT-INS the hostname directly into the body.
John DarbyPMAuthor Commented:
The clipboard...never knew how to do that. Thanks bro!
John DarbyPMAuthor Commented:
You rock, my friend...thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now