天天看點

awk 替換 比對

原文:http://liveforlinux.blog.51cto.com/3337218/741865

[[email protected] test]# cat awk

1a 9,100.34

1b 1,999.00

1c 5,656.55

[[email protected] test]# awk '{sub(/1/,"test")}{print "\n",$1,$2}' awk   

testa 9,100.34

testb 1,999.00

testc 5,656.55

[[email protected] test]# awk '{gsub(/1/,"test")}{print "\n",$1,$2}' awk  

testa 9,test00.34

testb test,999.00

testc 5,656.55

[[email protected] test]# awk '{sub(/[0-9]+/,"")}{print "\n",$1,$2}' awk 

a 9,100.34

b 1,999.00

c 5,656.55

列印出$1隻包含4個字元的 awk '$1~/^....$/{print $1}' file

http://bbs.linuxtone.org/thread-17620-1-1.html 看到的學習一下記錄一下 效果是有了 但時間和我系統時間對不上

[[email protected] test]# cat awk 

1a 9,100.34 dkjfjkdkjf 45  lopo

1b 1,999.00 dgfg       456 ll

1c 5,656.55 fghgf       465 df

[[email protected] test]# awk '{$2=strftime("%F %T",$2);print $1,$2,$3 >"bbb.txt";print $1,$2,$4 >"ccc.txt"}' awk

[[email protected] test]# cat bbb.txt 

1a 1969-12-31 16:00:09 dkjfjkdkjf

1b 1969-12-31 16:00:01 dgfg

1c 1969-12-31 16:00:05 fghgf

[[email protected] test]# date

Wed Dec 14 22:49:28 PST 2011

[[email protected] test]# cat ccc.txt 

1a 1969-12-31 16:00:09 45

1b 1969-12-31 16:00:01 456

1c 1969-12-31 16:00:05 465

[[email protected] test]# date

Wed Dec 14 23:07:09 PST 2011                                                            

  問題已解決 把{$2=strftime("%F %T",$2)中的$2去掉就可得到正确的格式了 見下圖

awk 替換 比對

 一個檔案,列數是不一樣的,如果有5列,就取前4列,如果有6列,就取前5列

awk 替換 比對

 當第一列大于2的時候 列印

awk 替換 比對