I'm using AIX 6.1 which isn't supporting in -B
and -A
flags:
grep: Not a recognized flag: B
Lets say I want to run:
cat file | grep -E -B4 'Directory entry type.*Indirect' | grep "Database name" | awk '{print $4}'
How can I do this kind of logic in AIX?
EDIT
My real code is as follow:
NAME_EXISTS=`db2 LIST DB DIRECTORY | grep -E -B5 'Directory entry type.*Remote' | grep "Database alias" | awk '{print $4}' | grep -i ${NAME} | wc -l`
if [ ${NAME_EXISTS} -gt 0 ]; then
db2 LIST DB DIRECTORY | grep -E -A5 "Database alias.*${NAME}"
fi
The idea is to find if there is a remote DB named $NAME
, if it finds it - show the 5 lines beginning Database alias.*${NAME}
. $NAME
is unique in Database alias
.
And the db2 LIST DB DIRECTORY
output something like this:
System Database Directory
Number of entries in the directory = 3
Database 1 entry:
Database alias = OLTPA
Database name = OLTPA
Local database directory = /db2/data
Database release level = 10.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 2 entry:
Database alias = OLTPF
Database name = OLTP
Node name = OLTPN
Database release level = 10.00
Comment =
Directory entry type = Remote
Catalog database partition number = -1
Alternate server hostname =
Alternate server port number =
Database 3 entry:
Database alias = ADMIN
Database name = ADMIN
Local database directory = /db2/data
Database release level = 10.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
For NAME=OLTPF
output will be:
Database alias = OLTPF
Database name = OLTP
Node name = OLTPN
Database release level = 10.00
Comment =
Directory entry type = Remote
For NAME=OLTPE
there will be no output.
-p
parameter is useful here. – EightBitTony Feb 16 '16 at 08:14Database name
always be exactly 4 lines beforeDirectory entry type.*Indirect
or can it be anywhere in the preceding 4 lines? – terdon Feb 16 '16 at 10:45