awk学习_2


今天准备实际测试一下昨天修改过的“自动屏蔽一些恶意IP”的文章,看看其中的脚本是否能够正常运行,先用echo代替了真实的iptables屏蔽,但是就是无法按照预想的正常工作:
awk '/ 404 /{status404[$1]++}; END {for (IP in status404) print status404[IP]"t", IP}' $log_file | sort -nr | awk '{if($1>$error_block) {print $2}}' | tee 404_ipList.txt    # $error_block 因为是外部变量,必须要用单引号引起来才行!!!

whitelist=$(last | grep -Ev '^$|mosh|reboot|wtmp' | awk '{print $3}' | sort | uniq | xargs)while read ip
do
	if ! $(echo ${whitelist} | grep -wq ${ip}); then
		echo "blocking ${ip}"
		#$IPTABLES -I INPUT -s ${ip}/32 -p tcp -m tcp --dport 80 -j DROP
	fi
done < 404_ipList.txt;

 

# awk调用外部变量是需要用“单引号”括起来。或者在前面用 -v 选项添加这个外部变量。
awk '{if($1>='$n') print $2}' file
参考网址:

下面是一些awk的实际例子,可以学习学习:
# {批量重命名}
ls junk* | awk '{print "mv "$0" ../iraf/"$0".dat"}' | bash
awk中的一些内置变量/函数:
# NF (number of columns){列的数量}
# NR (the current line that awk is working on)}{正在处理的当前行号}
# END (true if awk reaches the EOF)
# BEGIN (true before awk reads anything)
# length (number of characters in a line or a string)

 

# Print lines longer than 72 characters{打印长度超过72个字符的行}

awk 'length > 72' file

 

# Print length of string in 2nd column{打印第二列的字符串长度}
awk ‘{print length($2)}’ file

# Add up first column, print sum and average{对第1列做累加然后打印总和+平均值}

{ s += $1 } END{ print "sum is", s, " average is", s/NR }

 

# Print fields in reverse order{将每行内容逆序输出}

awk '{ for (i = NF; i > 0; --i) print $i }' file

 

# Print the last line{打印最后一行}

{line = $0} END{print line}

 

# {打印行中包含有Pat的总行数}

/Pat/ {nlines = nlines + 1} END{print nlines}

 

# Print all lines between start/stop pairs{打印在start和stop之间的所有行}

awk '/start/, /stop/' file

 

# Print column 3 if column 1 > column 2{如果第1列的值大于第2列,则打印第3列}

awk '$1 > $2 {print $3}' file

 

# Print line if column 3 > column 2{打印第3列大于第2列的行}
awk ‘$3 > $2’ file

# Count number of lines where col 3 > col 1 {打印第3列大于第1列的总行数}

awk '$3 > $1 {print i + "1"; i++}' file

 

# Print sequence number and then column 1 of file{打印序号以及每行的第1列}

awk '{print NR, $1}' file

 

# Print every line after erasing the 2nd field{不打印第2列}

awk '{$2 = ""; print}' file

 

# Print out 4 random numbers between 0 and 1{打印4个值在0、1之间的随机数}
yes | head -4 | awk ‘{print rand()}’

# Print out 4 random integers modulo 5{打印4个在0和5之间的随机数}

yes | head -4 | awk '{print int(100*rand()) % 5}'

# Replace every field by its absolute value{输出每一列的绝对值}
{ for (i = 1; i <= NF; i=i+1) if ($i < 0) $i = -$i print}

# Print frequency histogram of column of numbers{根据第2列队 值打印统计数据}
$2 <= 0.1 {na=na+1}
($2 > 0.1) && ($2 <= 0.2) {nb = nb+1}
($2 > 0.2) && ($2 <= 0.3) {nc = nc+1}
($2 > 0.3) && ($2 <= 0.4) {nd = nd+1}
($2 > 0.4) && ($2 <= 0.5) {ne = ne+1}
($2 > 0.5) && ($2 <= 0.6) {nf = nf+1}
($2 > 0.6) && ($2 <= 0.7) {ng = ng+1}
($2 > 0.7) && ($2 <= 0.8) {nh = nh+1}
($2 > 0.8) && ($2 <= 0.9) {ni = ni+1}
($2 > 0.9) {nj = nj+1}
END {print na, nb, nc, nd, ne, nf, ng, nh, ni, nj, NR}

# Find maximum and minimum values present in column 1{找出第1列中的最值,记得强制转换类型!}

NR == 1 {m=$1 ; p=$1}
$1+0 >= m+0 {m = $1}
$1+0 <= p+0 {p = $1}
END{ print "Max = " m, " Min = " p }

 

参考网址:http://sparky.rice.edu/~hartigan/awk.html


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注