Powershell grep and awk

13 March,2017 by Jack Vamvas

I use grep for exploring data sets   and awk for text processing on Linux. They are very powerful utilities which simplify  extracting and processing text.

For example , I have a CRON job running on command a Linux server which does something like:

Example 1

db2pd -d mydb -logs |grep 'Method 1 Archive Status' | awk '{print " JackV@myserver - MYDB "$3" "$4":  "$5}'

This command uses the db2pd utility to extract information from the db2pd logs output and process to return certain information from the data extracted

The output is :

JackV@myserver MYDB Archive Status:   Success

 

A question that regularly appears is how to achieve the same result in Powershell. In this case I’m referring to executing the db2pd utility on a DB2 LUW database on a Windows platform.

This is an example of using the Powershell command like to extract and present the text in the same output as the Linux grep and awk combination above. The example uses the select-string   cmdlet , similar to the grep process , and then creating an array using the split function

Example 2

db2pd -d MYDB -logs | select-string "Method 1 Archive Status" |%{$a=$_.ToString().split(" ");Write-Host "JackV@myserver" $a[0] $a[2] $a[3] $a[10]}

 

One difference to notice is the final column . In the awk example 1 – the final column is in the 5th position. In the Powershell Example 2 version the position is 10 . That’s because Powershell counts all the white spaces.

Read More

The great Windows or Linux debate for DB2 LUW

db2pd troubleshooting guide (DBA DB2)

 

 

 

Author: Jack Vamvas(http://www.dba-db2.com)

Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Powershell grep and awk

Comments are moderated, and will not appear until the author has approved them.


dba-db2.com | DB2 Performance Tuning | DBA DB2:Everything | FAQ | Contact | Copyright