Nigel Wash
Nigel Wash

Reputation: 107

using awk to print partly match in column

I am trying to use awk to print any matches that match a pattern in a specific column of data ($4) in a file stored in variable $file_log. The match has also got to be partial and case insenstive. This is the file data.

Protocol    Packets    Bytes    SourceIP
TCP         120        1000     EXT_SERVER
TCP         33         230      EXT_SERVER
UDP         260        157      100012_SERVER
ICMP        340        89       100012_SERVER

So if a user was to provide the input of 'ex' in the following code, the output would display all lines with EXT_SERVER under the $4 column.

read -p 'Type the Source IP code you wish to search for: ' src_ip_choice

This is what I have so far however there is no output, even though there is definitely data that matches the pattern I enter...

read -p 'Type the Source IP code you wish to search for: ' src_ip_choice
if grep -i -q "$src_ip_choice" "$specified_log"; then
    awk -F',' '$4~/$src_ip_choice/' $specified_log
else
    echo "The Source IP code you entered cannot be found within $specified_log"

P.S. I understand the column $4 does not need to be specified as 'EX' only appears in $4 anyway. However, this is a requirement.

Any help would be greatly appreciated!

Upvotes: 1

Views: 314

Answers (1)

anubhava
anubhava

Reputation: 784958

You may use this awk for ignore case match in 4th column:

s='[Ee][Xx]' # or read from user

awk -v re="$s" 'tolower($4) ~ tolower(re)' file

TCP         120        1000     EXT_SERVER
TCP         33         230      EXT_SERVER

$4 is 4th column and /[Ee][Xx]/ matches ex or EX or Ex or eX in 4th column.

Upvotes: 1

Related Questions