CyberKeeda In Social Media

AWK equivalent in Windows CMD




Missing AWK on Windows ?  for /f  may help you.
Lets see how can we implement the same.


Example.

C:\Users\Cyberkeeda>netsh interface show interface

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Wi-Fi
Enabled        Connected      Dedicated        VMware Network Adapter VMnet1
Enabled        Connected      Dedicated        VMware Network Adapter VMnet8
Enabled        Disconnected   Dedicated        Ethernet

Lets assume we have saved the same within a file in linux names as file.txt

Within Linux, to gather the State of interfaces, we use the below command.

cat file.txt| awk '{print $2}'

State
Connected
Connected
Connected
Disconnected

In Windows, it can be accomplished with FOR /F

C:\Users\cyberkeeda>for /f "tokens=2" %a in ('netsh interface show interface') do @echo %a

State
Connected
Connected
Connected
Disconnected

Now, if wish to add two values .

cat file.txt| awk '{print $1 " " $2}'

Admin State
-------------------------------------------------------------------------
Enabled Connected
Enabled Connected
Enabled Connected
Enabled Disconnected

Same can be accomplished within Windows as

C:\Users\cyberkeeda>for /f "tokens=1,2" %a in ('netsh interface show interface') do @echo %a %b
Admin State
-------------------------------------------------------------------------
Enabled Connected
Enabled Connected
Enabled Connected
Enabled Disconnected

In case, if you want to additional string along with the variable.

C:\Users\cyberkeeda>for /f "tokens=1,2" %a in ('netsh interface show interface') do @echo Boot Stats= %a Ethernet stat= %b


Now in case, if you want to use further filter within it, below one liner can help you.

C:\Users\cyberkeeda>netsh interface show interface | for /f "tokens=2" %a in ('findstr Wi-Fi') do @echo %a
Connected




5 comments:

  1. Very didactic explanation. Congrats!

    ReplyDelete
  2. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Ramen Antwerpen

    ReplyDelete
  3. This is helpful. but the problem if we use pipeline like example

    for /f "tokens=5" %a in ('tasklist | findstr edge') do @echo %a

    this will lead to a problem, so the only way is to write to file an rerun the for. It's quite bothersome tbh. but this tuts help me to open to new view of world that I never seen before. So yeah, thank you so much.

    I think the way to solve mu problem will be consist of printing the output to file and rerun the for like example

    C:\Temp\Dir> tasklist | findstr edge > temp.txt
    C:\Temp\Dir> for /f "tokens=5" %a in (temp.txt) do @echo %a

    Well if we use awk it will be faster with only one liner
    C:\Temp\Dir> tasklist | findstr edge | awk '{print $5}'

    Well nix and dos are different, and not every windows machine has awk, mine has one, so I can use it, but cmd is everywhere, and I won't bring awk everytime with me, so I really appreciate your article as workaround when there is no awk! Thank you so much!

    ReplyDelete
    Replies
    1. The author's very final example is useful for solving this problem. Run the tasklist first and pipe that to the for /f command like so:

      tasklist | for /f "tokens=5" %a in ('findstr edge') do echo %a

      Delete
  4. Shreesha Vitthala K AJune 5, 2023 at 11:25 AM

    Very succint and useful article

    ReplyDelete

Designed By Jackuna