Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

October 13, 2009 12:00 AM

Using Setx to Parse a Text File

A little tinkering provides a solution that beats the scripting route
Windows IT Pro
InstantDoc ID #102918
Rating: (0)

I recently needed to create a batch file that would give me an idea of whether my Internet connection was running well, so I thought I'd begin by doing a single ping to a particular DNS server on the Internet, redirecting that output to a text file, extracting the round-trip time in milliseconds, and displaying the results. (I figured the batch file could run in a little text window if I could minimize the output.) But how could I grab just a particular bit of text from an app's output? The Setx command provided the answer.

I introduced Setx—a command in Windows Vista and later (and in several older resource kits)—in "Essential Environment Variable Control with Setx", but I didn't get a chance to show you its ability to parse text out of input. Setx views a text file as a set of text lines, each of which contains words. It then lets you identify the particular word that you want to extract with two coordinates: the line that it's found in and its word position on that line. Setx counts from zero, not one, so the coordinates to tell Setx to grab the fifth word on the third line would be (4,2).

Setx is useful for extracting words out of text files that have regular structures, such as the captured output of a Ping command. (I've also used it to grab data out of Ipconfig outputs.) The command needs a text file to work with, so I'll create one for it by pinging my website once with the command

 ping -n 1 www.minasi.com>pr.txt 


But how do I tell Setx which line and word number identifies the reported round-trip time in milliseconds? You could start counting, of course, but that would be tedious. Instead, you can type

 setx /f pr.txt /x 


which causes Setx to dump all the words it can find in the text, along with their coordinates. For example, some of that output looks like

 (2,0 Reply)(2,1 from)(2,2 70.165.73.5:)(2,3 bytes=32)(2,4 time=30ms)(2,5 TTL=113)


This shows that in the phrase Reply from 70.165.73.5: bytes=32 time=30ms TTL=113, Setx found six "words": Reply, from, 70.165.73.5:, bytes=32, time=30ms, and TTL=113. The one I want, time=30ms, is prefixed with 2,4, meaning that it's the fourth word in the third line. Now I've got the information I need to tell Setx to take the captured output of a Ping command, extract the round-trip time, and put it into an environment variable that I'll call rtt:

 setx rtt /f pr.txt /a 2,4


It responds

 Extracted value: time=30ms.
SUCCESS: Specified value was saved.


That will get time=30ms into the environment variable rtt, and that might be all I need, but what if I don't want the time= part? Well, as you've probably guessed, Setx's idea of a "word" is a set of characters surrounded by the beginning of a line, a space, or the end of a line. (It also uses tabs as word delimiters.) To remove the 30 from time=30ms, I could use Setx's /d option to define extra delimiters (i.e., the equals sign and the letter m):

 setx rtt /f pr.txt /d "m","=" /a ...


But that /a option needs coordinates, and I've rearranged the whole coordinate system quite a bit. How do I determine where 30 shows up with these new delimiters? I'd use another /x command, but this time with the delimiters in place:

 setx /f pr.txt /d "m","=" /x 


Believe me, that command results in a real mess. To thin the output a bit, I can filter out all the lines except for the ones that contain 30, like so:

 setx /f pr.txt /x /d "=","m"|findstr "30"


That shows just two lines:

 (2,0 Reply)(2,1 fro)(2,2 70.165.73.5:)(2,3 bytes)(2,4 32)(2,5 ti)(2,6 e)(2,7 30)
(2,8 s)(2,9 TTL)(2,10 113)(7,0 Mini)(7,1 u)(7,2 30)(7,3 s)
(7,4 Maxi)(7,5 u)(7,6 30)(7,7 s)(7,8 Average)(7,9 30)(7,10 s)


The 30 I'm looking for is in the first of those two lines—yes, it's a bit uglier to read because Setx has removed the letter m—and so the coordinates I want are (2,7).

Now I have a command that will pull out the roundtrip time:

 setx rtt /f pr.txt /d "m","=" /a 2,7


And finally, I get the result

 Extracted value: 30.
SUCCESS: Specified value was saved.


If you're putting together something that winnows out just a few bits of information, and you don't feel like scripting, give Setx a try. It might save you some time.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.