awk 命令的基本格式: awk [pattern] [action] input
[pattern] 为:正则表达式、模式匹配、范围区间、关系表达式
本文主要通过具体的测试文件,直接通过不同的脚本命令快速体验下各个pattern的模式。
一、先准备测试文件test.data,内容如下:
vi test.data :
abcd zzxx hello 15 1
8si test nihao 20 3
lksse chyu world 10 10
ljssdf xfeng testhello 6 3
x0807x xfeng testhello 6 1
slkddddgg wwling nihaonihao 30 15
9298 zlei helloworld 9 0
ow9k dweiwe testhello 8 0
iwwe jiangong qianmingg 13 1
dingnc ddx tech 23 end
iidingnc ddx tech 10 end
二、通过具体的测试脚本进行快速体验
测试脚本1(shell)
正则表达式 & 模式匹配
echo "--------------------正则表达式: ------------------"
echo "awk '/xfeng/ {print \$0}' test.data "
echo "------>"
awk '/xfeng/ {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 正则表达式: ------------------"
echo "awk '/^[0-9]/ {print \$0}' test.data "
echo "------>"
awk '/^[0-9]/ {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "--------------------模式匹配: ------------------"
echo "awk '\$1~/0807x/ {print \$0}' test.data "
echo "------>"
awk '$1~/0807x/ {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 模式匹配: ------------------"
echo "awk '\$3~/testhello/ {print \$0}' test.data "
echo "------>"
awk '$3~/testhello/ {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 模式匹配: ------------------"
echo "awk '\$3!~/testhello/ {print \$0}' test.data "
echo "------>"
awk '$3!~/testhello/ {print $0}' test.data
echo "------------------------------------------------"
测试脚本2(shell)
范围区间
echo "-------------------- 范围区间: ------------------"
echo "awk '/zzxx/,/xfeng/ {print \$0}' test.data "
echo "------>"
awk '/zzxx/,/xfeng/ {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 范围区间: ------------------"
echo "awk '/^dingnc/,/end$/ {print \$0}' test.data "
echo "------>"
awk '/^dingnc/,/end$/ {print $0}' test.data
echo "------------------------------------------------"
测试脚本3(shell)
关系表达式
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$4 > 10 {print \$0}' test.data "
echo "------>"
awk '$4>10 {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$4+\$5< 10 {print \$0}' test.data "
echo "------>"
awk '$4+$5<10 {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$4==20 {print \$0}' test.data "
echo "------>"
awk '$4==20 {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$3==\"nihaonihao\" {print \$0}' test.data "
echo "------>"
awk '$3=="nihaonihao" {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$4==20 || \$4<10 {print \$0}' test.data "
echo "------>"
awk '$4==20 || $4<10 {print $0}' test.data
echo "------------------------------------------------"
echo ""
echo ""
echo "-------------------- 关系表达式: ------------------"
echo "awk '\$4!=20 && \$4!=10 {print \$0}' test.data "
echo "------>"
awk '$4!=20 && $4!=10 {print $0}' test.data
echo "------------------------------------------------"