使用awk打印列中的部分匹配项 [英] using awk to print partly match in column
问题描述
我正在尝试使用awk在存储在变量$ file_log中的文件中的特定数据列($ 4)中打印与模式匹配的所有匹配项.比赛也必须是局部的并且不区分大小写.这是文件数据.
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
因此,如果用户要在下面的代码中提供"ex"的输入,则输出将在$ 4列下显示所有带有EXT_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.我知道不必指定$ 4列,因为无论如何"EX"仅出现在$ 4中.但是,这是必需的.
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.
任何帮助将不胜感激!
推荐答案
您可以在第4列中使用此 awk
忽略大小写匹配:
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
是第4列,/[Ee] [Xx]/
匹配 ex
或 EX
或<第四列中的code> Ex 或 eX
.
$4
is 4th column and /[Ee][Xx]/
matches ex
or EX
or Ex
or eX
in 4th column.
这篇关于使用awk打印列中的部分匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!