Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Powershell grep and awk

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)



This post first appeared on DBA-DB2.com, please read the originial post: here

Share the post

Powershell grep and awk

×

Subscribe to Dba-db2.com

Get updates delivered right to your inbox!

Thank you for your subscription

×