CyberKeeda In Social Media
Showing posts with label Scripts. Show all posts
Showing posts with label Scripts. Show all posts

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




Read more ...

Python For Loop cheat sheet





A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

  • Loop over a single list with a regular for-in:

     liquors = ["rum", "beer", "whisky", "vodka"] 
     for my_liquor in liquors:
           print("Hello, Lets have " + my_liquor + " today")

Save the file , i named it as alcohol.py and run it from terminal.
  #  python alcohol.py

 Hello, Lets have rum today
 Hello, Lets have beer today
 Hello, Lets have whisky today
 Hello, Lets have vodka today


  • Loop over a single word for-in:

   for letters in "hello":
         print(letters)

Save the file , i named it as demo1.py and run it from terminal.
# python demo1.py

h
e
l
l
o

  • Loop over a single list with a break statement
     liquors = ["rum", "beer", "whisky", "vodka"] 
     for my_liquor in liquors:
           print("Hello, Lets have " + my_liquor + " today")
           if  my_liquor == "whisky":
               break


Save the file , i named it as demo2.py and run it from terminal.
# python demo2.py

Hello, Lets have rum today
Hello, Lets have beer today
Hello, Lets have whisky today

  • Loops over multiple lists

liquors = ["rum", "beer", "whisky", "vodka"]
customers= ["John", "Sam", "Jack", "Ria"]
for liquor, customer in zip(liquors, customers):
    print( customer + " will have " + liquor)


Save the file , i named it as demo3.py and run it from terminal.


# python demo3.py

John will have rum
Sam will have beer
Jack will have whisky
Ria will have vodka

Read more ...
Designed By Jackuna