-bash-3.2$ awk '{ split( "20:18:00", time, ":" ); print time[2] }'
-bash-3.2$ awk '{ split( "20:18:00", time, ":" ); print time[2] }' feng.txt
18
18
18
18
18
18
-bash-3.2$ awk 'BEGIN{ split( "20:18:00", time, ":" ); print time[2] }'
18
-bash-3.2$ wc -l feng.txt
6 feng.txt
-bash-3.2$
-bash-3.2$ cat feng.txt
k 2012-1-3 13:00:46
a 2012-1-3 18:07:59
d 2012-1-3 18:07:59
b 2012-1-3 9:01:48
b 2012-1-3 13:00:46
b 2012-1-3 18:07:59
-bash-3.2$ cat feng.txt |awk '{gsub(/2012/,"yangkai")print }'
awk: {gsub(/2012/,"yangkai")print }
awk: ^ syntax error
-bash-3.2$ cat feng.txt |awk '{gsub(/2012/,"yangkai");print }'
k yangkai-1-3 13:00:46
a yangkai-1-3 18:07:59
d yangkai-1-3 18:07:59
b yangkai-1-3 9:01:48
b yangkai-1-3 13:00:46
b yangkai-1-3 1
-bash-3.2$ cat feng.txt |awk '{{gsub(/1/,"yk"),$3}print }'
awk: {{gsub(/1/,"yk"),$3}print }
awk: ^ syntax error
-bash-3.2$ cat feng.txt |awk '{gsub(/1/,"yk"),$3};print }'
awk: {gsub(/1/,"yk"),$3};print }
awk: ^ syntax error
awk: {gsub(/1/,"yk"),$3};print }
awk: ^ syntax error
-bash-3.2$ cat feng.txt |awk '{gsub(/1/,"yk"),$3}print }'
awk: {gsub(/1/,"yk"),$3}print }
awk: ^ syntax error
awk: {gsub(/1/,"yk"),$3}print }
awk: ^ syntax error
-bash-3.2$ cat feng.txt |awk '{gsub(/1/,"yk",$3);print }'
k 2012-1-3 yk3:00:46
a 2012-1-3 yk8:07:59
d 2012-1-3 yk8:07:59
b 2012-1-3 9:0yk:48
b 2012-1-3 yk3:00:46
b 2012-1-3 yk8:07:59
-bash-3.2$
sub函数匹配记录中最大、最靠左边的子字符串的正则表达式,并用替换字符串替换这些字符串。如果没有指定目标字符串就默认使用整个记录。替换只发生在第一次匹配的时候.
gsub函数作用如sub,但它在整个文档中进行匹配。
-bash-3.2$ cat feng.txt |awk '{sub(/1/,"yk",$3);print }'
k 2012-1-3 yk3:00:46
a 2012-1-3 yk8:07:59
d 2012-1-3 yk8:07:59
b 2012-1-3 9:0yk:48
b 2012-1-3 yk3:00:46
b 2012-1-3 yk8:07:59
-bash-3.2$ cat feng.txt |awk '{sub(/1/,"yk");print }'
k 20yk2-1-3 13:00:46
a 20yk2-1-3 18:07:59
d 20yk2-1-3 18:07:59
b 20yk2-1-3 9:01:48
b 20yk2-1-3 13:00:46
b 20yk2-1-3 18:07:59
-bash-3.2$
length函数返回记录的字符数。
-bash-3.2$ awk 'BEGIN{print length("yangkai")}'
7
-bash-3.2$
awk 'BEGIN{FS=OFS=":"}{if($3<=50)$3="500";print}
阅读(3411) | 评论(0) | 转发(0) |