I have a file data which are trying to filter via awk, i am able to filter the data but want the awk statement to be simpler into one line:
File contents:
Entity Name
Value
Unknown dbs636294051.klm.bet.com: /opt
N/A
Unknown dbs636294051.klm.bet.com: /tmp
N/A
Unknown dbs636294051.klm.bet.com: /var
N/A
My trial:
awk '!/^N/{ if($2 ~ /klm/) print $2}' file | awk -F":" '{print $1}'
The above works but i'm looking if this can be trimmed to the before pipe line:
dbs636294051.klm.bet.com
dbs636294051.klm.bet.com
dbs636294051.klm.bet.com
CodePudding user response:
You can write a single awk command, setting the field separator to 1 or more spaces or :
, check if field 1 does not start with N
ad that it does contain klm
To be really specific, you could also write ^N\/A$
Thanks to the comments of @Renaud Pacalet and @Wiktor Stribiżew the command can look like:
awk -F'[[:blank:]] |:' '!/^N/ && $2 ~ /klm/{print $2}' file
In parts
awk -F'[[:blank:]] |:' ' # Set the field separator to either 1 spaces or tabs or a semicolon
!/^N/ && $2 ~ /klm/ # If the record does not start with `N` and field 2 does contain klm
{print $2} # Print the second column
Output
dbs636294051.klm.bet.com
dbs636294051.klm.bet.com
dbs636294051.klm.bet.com
CodePudding user response:
The sub
function can be used to trim the colon and anything following it from $2
:
awk '!/^N/ && $2 ~ /klm/ {sub(/:.*$/,"",$2); print $2}' file
CodePudding user response:
It's a quick and dirty one, which works for the given example. If you have more filter rules, it's also easy to adjust.
awk -F'[:\\s]' 'NR>1 && $2~/klm/{print $2}' f
636294051.klm.bet.com
636294051.klm.bet.com
636294051.klm.bet.com
Update, another approach:
awk '$2~/klm/ && (($0=$2) sub(/:.*/,""))' f
CodePudding user response:
awk '/Unknown/{gsub(/:/,"",$0);print $2}' file
CodePudding user response:
When you have two piped awk
commands with different field separators like
awk '!/^N/{ if($2 ~ /klm/) print $2}' file | awk -F":" '{print $1}'
you might use split
function to turn that into single awk
commands, in this case
awk '!/^N/{ if($2 ~ /klm/){split($2,arr,":");print arr[1]}}' file
Disclaimer: this answer pertains solely for changing 2 awk
s into single, other ways to ameliorate are outside scope of this answer.