Awk結合Shell 往MySQL表中寫資料吧!

來源:互聯網
上載者:User

需求:

1、每天根據日期在test庫中建立chinacache_0726(即:chinacache_日期);

2、根據表結構處理記錄檔13039_20100722_w3c,並把結果插入到建立的表中; 

awk的功能不只是簡單的處理一些資料而已,看了下面的代碼,結合shell,你將看到不一樣的awk

代碼:

!/usr/bin/awk -f

#sh.awk

BEGIN{
        "date +%m%d" | getline today
        cdn="chinacache"
        system("/usr/local/mysql/bin/mysql -uroot -proot test -e \"create table chinacache_`date +%m%d` (id int(20) NOT NULL AUTO_INCREMENT,code int(20),ip varchar(20),times int(20),cdn_name varchar(20),file varchar(256),url varchar(256),access_time varchar(30),PRIMARY KEY (id))\"")
}
{
        split($4,a,":")
        b[$1" "$7" "$9" "$11" ",a[2]":"a[3]]++
}

END{
        for(i in b){
                split(i,c," ")
                printf("insert into `%s` (code,ip,times,cdn_name,file,url,access_time) values ('%s','%s','%s','%s','%s','%s','%s');\n",cdn"_"today,c[3],c[1],b[i],cdn,c[2],c[4],c[5]);
        }

寫好需求寫好了代碼,下面,可以結合shell把資料插入到資料庫的表中:

./sh.awk /data/log/13039_20100722_w3c |/usr/local/mysql/bin/mysql -uroot -proot test

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.