Use the Shell to write data to the MySQL table!

Source: Internet
Author: User
Requirement: 1. Create chinache_0726 (chinacache _ date) in the test database every day (I .e., chinacache _ date); 2. process the log file 13039_20100722_w3c according to the table structure

Requirement: 1. Create chinache_0726 (chinacache _ date) in the test database every day (I .e., chinacache _ date); 2. process the log file 13039_20100722_w3c according to the table structure

Requirements:

1. Create chinache_0726 (chinacache _ date) in the test database every day );

2. process the log file 13039_20100722_w3c according to the table structure and insert the result to the created table;

The awk function is not just a simple process of some data. After reading the following code, combined with shell, you will see a different awk

Code:

! /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, ,":")
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'); \ n ", cdn" _ "today, c [3], c [1], B [I], cdn, c [2], c [4], c [5]);
}
}

Write the required code, and insert the data to the database table using shell:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.