Conditional awk - for when grep is finding duplicates

>> Friday 12 July 2019

My files that contain information about my databases often look like this (It's a dirty way to maintain a database inventory on a server with multiple databases when the database product itself doesn't have a concept of inventory):


Mydatabasename:/path/to/product/home:/path/to/conf/myfile.conf
Anotherdatabasename:/path/to/product/home:/path/to/conf/myfile2.conf


When the database name appears in 2 lines of the file a simple grep would bring back multiple lines:

mydb=$1
DBHome=grep -w $1 ${myfilelistingdbs}| awk -F ":" '{print $2}'



So do THIS instead:

mydb=$1
awk -F ":" -vx=$mydb '$1==x {print $2}' ${myfilelistingdbs}


It says: if the first field matches this then tell me second field contents

this solves problem of db name popping up in a home path

e.g
database_cola:/data/database_pepsi/product:/data/database_pepsi/conf/database_cola.conf
database_pepsi:/data/database_pepsi/product:/data/database_pepsi/conf/database_pepsi.conf


A grep on database_pepsi returned two product homes (and upset my scripts expecting only one line!)
Awk on first field means you only get one product home returned

0 comments:


  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP