
これらの変数のいずれかが該当する場合は、コードを印刷したいと思います。これは私の実際のコードではありません。これは単なる例です。
read -p "enter protocol: " protocol
read -p "enter src ip: " srcip
read -p "enter dst ip: " dstip
read -p "enter src port: " srcport
read -p "enter dst port: " dstport
など。
awk -F"," -v pro="$protocol" -v sip="$srcip" -v dip="$dstip" -v sport="$srcport" -v dport="$dstport" '{ if(pro == "tcp" && sip == "10" && dip == "30" && sport == "4" && dport == "1")
print $1,$2,$3,$4,$5,$6,$7}' test.txt > test2.txt
もう少し明確にするために、ここでは別の方法で作成します。
PROTOCOL,SRC IP,SRC PORT,DEST IP,DEST port
tcp .10 29 .30 300
udp .34 545 .94 90
tcp .23 233 .23 42
ユーザー入力と一致するには、この変数のうち3つが必要です。行を印刷します。
以下のコードを試しましたが、エラーが発生します。
awk -F"," -v pro="$protocol" -v sip="$srcip" -v dip="$dstip" -v sport="$srcport" -v dport="$dstport" 'BEGIN {if ($3 ~ pro) count++; if ($4 ~ sip) count++; if ($5 == sport) count++; if ($6 ~ dip) count++; if ($7 == dport) count++; count>=3; { print $3,$4,$5,$6,$7 }' test.txt > test2.txt
error: ^ syntax error
^ unexpected newline or end of string
答え1
テストされていません:
awk -F',' -v pro="$protocol" -v sip="$srcip" -v dip="$dstip" -v sport="$srcport" -v dport="$dstport" '
{
c = 0
c += (pro == "tcp")
c += (sip == "10")
c += (dip == "30")
c += (sport == "4")
c += (dport == "1")
}
c > 2
' test.txt
c > 2 { print $1,$2,$3,$4,$5,$6,$7 }
行全体ではなくフィールドのサブセットのみを印刷したい場合は、これを行います。
答え2
#!/bin/bash
read -p "enter the src ip:" srcip
read -p "enter the des ip:" desip
read -p "enter the src port:" srcport
awk -v srcip="$srcip" -v desip="$desip" -v srcport="$srcport" '($2 == srcip||$4 == desip || $5 == srcport){print $0}' filename
テストを経てうまく機能しました
出力
src ip:.10 と入力します。 des ip:.30と入力します。 srcポート:34と入力してください。
TCP .10 29 .30 300