执行:echo "a-b:c>d,e f, ]" '{print $1,$2,$3,$4,$5,$6}'
结果:a b c d e f
说明:[-:>, ],awk取值时同时以这些符号分割
1.间隔4秒一直写入数据
while true;do echo `date` >> /etc/puppet/modules/motd/files/etc/motd; sleep 4;done
2.去掉'#'修改IP为本机IP
touch /tmp/zzg
cat >>/tmp/zzg<< EOF
#server_address=127.0.0.1
EON
取IP赋值给a
a=`ifconfig eth0|grep "inet addr:"|awk -F "[ :]+" '{print $4}'`
或者
a=`ifconfig eth0|grep "inet addr:"|awk -F ":" '{print $2}'|awk -F " " '{print $1}'`
替换
eval sed -i 's/#server_address=.*/server_address=`echo $a`/' /tmp/zzg
cat /tmp/zzg
server_address=192.168.0.132
3.字符串 str="in:ADSL out:Lan, proto TCP (ACK,PSH), 60.28.123.144:6020->192.168.1.70:1538, len 64' );"
在str中60.28.123.144表示源地址,6020表示源端口,192.168.1.70表示目的地址,1538表示目的端口
我的目的是想将地址和端口分别保存在$SRCIP,$SRCPORT,$DESIP,$DESPORT等变量中。
str="in:ADSL out:Lan, proto TCP (ACK,PSH), 60.28.123.144:6020->192.168.1.70:1538, len 64' );"
用eval给4个变量赋值
eval `echo $str |awk -F"[- :>,]" '{print "SRCIP="$11,"SRCPORT="$12,"DESIP="$14,"DESPORT="$15}'`
或者
eval $(echo $str|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:[0-9]\+'|xargs|awk -F'[: ]' '{print "si="$1,"sp="$2,"di="$3,"dp="$4}')
命令分解:
1) echo $str|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:[0-9]\+'
60.28.123.144:6020
92.168.1.70:1538
2) echo $str|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}:[0-9]\+'|xargs
60.28.123.144:6020 92.168.1.70:1538
4. cat >>/tmp/file<
43:08-13 18:09:32.879 129 223 I RILR : CChannel::ProcessModemData() - INFO: chnl=[4] size=[44] RX [+XREG: 1,8,BAND_UMTS_I,"A011","00214B15"]
128:08-13 18:09:37.149 129 223 I RILR : CChannel::ProcessModemData() - INFO: chnl=[4] size=[44] RX [+XREG: 1,2,BAND_UMTS_I,"A011","00214B15"]
EOF
通过awk显示以下2行参数
cat /tmp/file|awk -F'[ =<>(),"]' '{print $1,$2,$37}'
43:08-13 18:09:32.879 00214B15
128:08-13 18:09:37.149 00214B15
阅读(1386) | 评论(0) | 转发(0) |