How to Display Nth Column of a File or an Output using Windows Command Line? – With Examples

Working with large data sets can be a daunting task, especially when you need to extract specific information from them. One way to make this process more manageable is by learning how to display only the columns that you need. In this blog post, we will explore how to display the Nth column of a file or an output using the Windows command line.

The FOR /F Command

The FOR /F command is a versatile tool that allows you to loop through the lines of a file or the output of a command. It can be used to extract specific information from each line by using the “delims=” and “tokens=” options.

  • The “delims=” option is used to specify the delimiter that separates the different fields in the file or output. The most common delimiter is a space, but it can also be a tab, a comma, or any other character.
  • The “tokens=” option is used to specify which field or token you want to extract from each line. By default, the FOR /F command will extract the first token, but you can also specify other tokens by using a comma-separated list.

Displaying the Nth Column of a File

To display the Nth column of a file, you can use the FOR /F command along with the “delims=” and “tokens=” options. Here is an example that shows how to display the 3rd column of a file called “data.txt”:

for /f “delims=, tokens=3” %i in (data.txt) do echo %i

In this example, we are using a comma as the delimiter, and we are specifying that we want to extract the 3rd token or column. The “%i” variable is used to store the current token, and the “echo %i” command is used to display it.

Here are some other examples of how to display the Nth column of a file using different delimiters:

  • To display the 2nd column of a file that uses spaces as the delimiter:

    for /f “tokens=2” %i in (data.txt) do echo %i

  • To display the 4th column of a file that uses tabs as the delimiter:

    for /f “delims= tokens=4” %i in (data.txt) do echo %i

  • To display the 5th column of a file that uses semicolons as the delimiter:

    for /f “delims=; tokens=5” %i in (data.txt) do echo %i

Displaying the Nth Column of an Output

How to Display Nth Column of a File

If you want to display the Nth column of output from another command, you can use a pipe (“|”) to redirect the output to the FOR /F command. Here is an example that shows how to display the 2nd column of the output from the “dir” command:

dir | for /f “delims= ” tokens=2″ %i in (‘dir /b’) do echo %i

In this example, we are using a space as the delimiter, and we are specifying that we want to extract the 2nd token or column. The “dir /b” command is used to display a list of files and directories in the current directory in a bare format, which is easier to parse.

Here are some other examples of how to display the Nth column of output using different commands and delimiters:

  • To display the 3rd column of the output from the “netstat” command:

    netstat | for /f “delims= ” tokens=3″ %i in (‘netstat’) do echo %i

  • To display the 4th column of the output from the “ping” command:

    ping google.com | for /f “delims= ” tokens=4″ %i in (‘ping google.com’) do echo %i

  • To display the 1st column of the output from the “tracert” command:

    tracert google.com | for /f “tokens=1” %i in (‘tracert google.com’) do echo %i

It’s important to note that the examples above are just basic examples and the delimiter and token number may need to be adjusted based on the format of the file or output you are working with.

Conclusion

In conclusion, the FOR /F command is a powerful tool that allows you to extract specific information from large data sets. By using the “delims=” and “tokens=” options, you can display the Nth column of a file or an output with ease. With the examples provided in this blog post. One should have a good starting point for working with your own data sets.

Also Read: How to Install Issabel on CentOS 7 | Install in Just 5 Easy Steps

Leave a Comment