user2023
user2023

Reputation: 478

Awk based filtering of data on a file in Linux

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

Upvotes: 0

Views: 640

Answers (5)

Daweo
Daweo

Reputation: 36450

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 awks into single, other ways to ameliorate are outside scope of this answer.

Upvotes: 1

awk '/Unknown/{gsub(/:/,"",$0);print $2}' file

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163352

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

Upvotes: 1

Kent
Kent

Reputation: 195059

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

Upvotes: 1

Renaud Pacalet
Renaud Pacalet

Reputation: 29050

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

Upvotes: 3

Related Questions