Cyberithub

How to Display Nth Column of a File or an Output using Windows Command Line

Advertisements

In this article, we will see how to display nth column of a File or an Output using Windows Command Line with the help of some real world examples. Very often, we do face a situations where we just need to display few columns from a file or from a command output in Windows. May be even delete a column from a file or from an output. All these things are quite easily possible if you are using Linux or Unix based Systems what about when you are using Windows based systems.

Well, there are no such tools like awk and sed in Windows like we have in Linux or Unix, so to extract the column from a file or a command output we can either use inbuilt PowerShell script or Windows commands. Here I will show you how to perform column operations in Windows using just for loop so that you can use it in your System irrespective of the Windows version you are using. More about for loop in Windows.

How to Display Nth Column of a File or an Output using Windows Command Line

How to Display Nth Column of a File or an Output using Windows Command Line

Also Read: 31 Most Useful netsh command examples in Windows

Example 1: How to Display the 1st Column of an Output

If you have any command with output in the form of rows and columns like we have for docker ps -a command here.

C:\>docker ps -a
CONTAINER ID IMAGE                  COMMAND             CREATED            STATUS                         PORTS    NAMES
c2adf7864497 debian:latest          "/bin/bash"         10 seconds ago     Exited (0) 5 seconds ago                competent_tesla
b47e78d48042 bitnami/jsonnet        "jsonnet /bin/bash" 20 seconds ago     Exited (1) 19 seconds ago               quirky_grothendieck
201b2ce8cb1d bitnami/jsonnet:latest "jsonnet /bin/bash" 33 seconds ago     Exited (1) 33 seconds ago               relaxed_golick
a51fdaa8be5d debian:latest          "/bin/bash"         50 seconds ago     Exited (0) 47 seconds ago               infallible_lewin
8ec360b2132c ubuntu:latest          "bash"              About a minute ago Exited (0) About a minute ago           sleepy_chatterjee
c4fbb13caa7f ubuntu:latest          "/bin/bash"         About a minute ago Exited (0) About a minute ago           peaceful_thompson

Then to display the 1st column from above output, you need to use for /f "tokens=1 delims= " %i in ('docker ps -a') DO @echo %i as shown below.

C:\>for /f "tokens=1 delims= " %i in ('docker ps -a') DO @echo %i
CONTAINER
c2adf7864497
b47e78d48042
201b2ce8cb1d
a51fdaa8be5d
8ec360b2132c
c4fbb13caa7f

 

Example 2: How to Display the 3rd Column of an Output

Similarly, if you have some other command output like docker images output.

C:\>docker images
REPOSITORY      TAG    IMAGE ID     CREATED     SIZE
debian          latest 827e5611389a 5 hours ago 124MB
bitnami/jsonnet latest 9c99af3c6e63 7 hours ago 87.6MB
ubuntu          latest ba6acccedd29 4 weeks ago 72.8MB

Then to display the 3rd column from above output, you need to use for /f "tokens=3 delims= " %i in ('docker images') DO @echo %i as shown below.

C:\>for /f "tokens=3 delims= " %i in ('docker images') DO @echo %i
IMAGE
827e5611389a
9c99af3c6e63
ba6acccedd29

 

Example 3: How to Display the 1st and 3rd Column of an Output

To display the 1st and 3rd column of docker images command output, you need to use for /f "tokens=1,3 delims= " %i in ('docker images') DO @echo %i %j as shown below.

C:\>for /f "tokens=1,3 delims= " %i in ('docker images') DO @echo %i %j
REPOSITORY IMAGE
debian 827e5611389a
bitnami/jsonnet 9c99af3c6e63
ubuntu ba6acccedd29

 

Example 4: How to Display a range of Columns from an Output

Sometimes you might want to see a range of columns from an output. To do that you can assign the column range to tokens. For example, here we are displaying column from 1st to 3rd using for /f "tokens=1-3 delims= " %i in ('docker images') DO @echo %i %j %k as shown below.

C:\>for /f "tokens=1-3 delims= " %i in ('docker images') DO @echo %i %j %k
REPOSITORY TAG IMAGE
debian latest 827e5611389a
bitnami/jsonnet latest 9c99af3c6e63
ubuntu latest ba6acccedd29

 

Example 5: How to Display the 1st Column of a File

In the previous examples, we were showing columns from a command output but what if we have to show columns from a file instead from a command output. In that case, we need to use the file name in our command. Here we have copied the output of docker images command to file.txt and then displaying the columns from it. So to display the 1st column from file.txt, you need to use for /f "tokens=1 delims= " %i in (file.txt) DO @echo %i as you can see below.

C:\>for /f "tokens=1 delims= " %i in (file.txt) DO @echo %i
REPOSITORY
debian
bitnami/jsonnet
ubuntu

 

Example 6: How to Display the 1st and 2nd Column of a File

Similarly, to display the 1st and 2nd column from file.txt file, you need to use for /f "tokens=1,2 delims= " %i in (file.txt) DO @echo %i %j command. So here if you notice inside the parentheses we just have given the file name.

C:\>for /f "tokens=1,2 delims= " %i in (file.txt) DO @echo %i %j
REPOSITORY TAG
debian latest
bitnami/jsonnet latest
ubuntu latest

 

Example 7: How to Display a range of Column from a File

Likewise, to display a range of columns i.e from 1st to 3rd column of file.txt, you need to use for /f "tokens=1-3 delims= " %i in (file.txt) DO @echo %i %j %k as shown below.

C:\>for /f "tokens=1-3 delims= " %i in (file.txt) DO @echo %i %j %k
REPOSITORY TAG IMAGE
debian latest 827e5611389a
bitnami/jsonnet latest 9c99af3c6e63
ubuntu latest ba6acccedd29

 

Example 8: How to Delete the 1st Column of an output

Till now, we have looked about displaying a column from a file or an output. But what if you need to delete an entire column from an output. In that case you need to change your command a bit. To delete a column, you need to provide the delete command in DO section. For example, here we are deleting 1st column of docker ps -a command which basically contains all the Container ID using for /f "tokens=1 delims= " %i in ('docker ps -a') DO docker rm %i. So here if you notice we have changed the command in DO section. Instead of echo we are using docker rm command.

C:\>for /f "tokens=1 delims= " %i in ('docker ps -a') DO docker rm %i

C:\>docker rm CONTAINER
Error: No such container: CONTAINER

C:\>docker rm c2adf7864497
c2adf7864497

C:\>docker rm b47e78d48042
b47e78d48042

C:\>docker rm 201b2ce8cb1d
201b2ce8cb1d

C:\>docker rm a51fdaa8be5d
a51fdaa8be5d

C:\>docker rm 8ec360b2132c
8ec360b2132c

C:\>docker rm c4fbb13caa7f
c4fbb13caa7f

If you now verify the list of containers, then you can see that all the containers are deleted.

C:\>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

 

Example 9: How to Delete 3rd Column of an Output

Similarly, if you choose to delete 3rd column of a command output then you need to use for /f "tokens=3 delims= " %i in ('docker images') DO docker rmi %i as shown below.

C:\>for /f "tokens=3 delims= " %i in ('docker images') DO docker rmi %i

C:\>docker rmi IMAGE
Error response from daemon: invalid reference format: repository name must be lowercase

C:\>docker rmi 827e5611389a
Untagged: debian:latest
Untagged: debian@sha256:e8c184b56a94db0947a9d51ec68f42ef5584442f20547fa3bd8cbd00203b2e7a
Deleted: sha256:827e5611389abf13dad1057e92f163b771febc0bcdb19fa2d634a7eb0641e0cc
Deleted: sha256:a36ba9e322f719a0c71425bd832fc905cac3f9faedcb123c8f6aba13f7b0731b

C:\>docker rmi 9c99af3c6e63
Untagged: bitnami/jsonnet:latest
Untagged: bitnami/jsonnet@sha256:46a2848ace4d46793f7114335efec1c315e52269407b1be853f3f627f9c105be
Deleted: sha256:9c99af3c6e635fbbdcb597e6f46a6787be252af81b70e52329f142219f07b6df
Deleted: sha256:a8f2fe465f552c83feee9c0d7ad48a638cfc57dae90b288acd4d2c2525a5f038
Deleted: sha256:034b3216b89867b7678e41ea8d94578a09d6a3370769909db1329232ca0ebfe5
Deleted: sha256:22e06ef139971d7fcd8b4ae612c99d9782f8191a13fc24f70bc361652be30aaf
Deleted: sha256:71d8362ca36682d304d5e782ed0410cc988fff2f179c7cfdf9dad3ff916e5bfb
Deleted: sha256:a3fa09d727ccdd81d8a077e7e8e65b15f9a4e062062be5f0fea852fc65a71905
Deleted: sha256:64962e71e93c203569b5a03f8d8ca72435d7e26596ba8d682ff44727dc5cbb0d

C:\>docker rmi ba6acccedd29
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Deleted: sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1
Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b

If you now verify all the docker images then you can see that all of them are deleted.

C:\>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE

Leave a Comment