shell - awk && gsub 的一些使用备忘

#!/bin/bash
#desc:   insert time into database
#date:   2018-01-15
#auth:   cai

HOSTNAME="localhost"
USERNAME="user"
PASSWORD="password"
DBNAME="dbftp"
TABLENAME="ftpfile"
TABLENAME_rec="ftptable"

#show tables of database
#show_db="show databases"
#mysql -h${HOSTNAME} -u${USERNAME} -p${PASSWORD} -e "${show_db}"

#create a temp table
#table_tmp=${TABLENAME}_`date "+%Y%m%d"`
create_table_sql="drop table if exists ${TABLENAME};create table IF NOT EXISTS ${TABLENAME}(id int(10) not null auto_increment primary key,date varchar(40),channel varchar(40),time varchar(40),program varchar(40),record int)"
mysql -h${HOSTNAME} -u${USERNAME} -p${PASSWORD} -D ${DBNAME} -e "${create_table_sql}"


#scanf the Folder files and insert  the date and channel into the temp table

function ReadTables()
{
cd '/root/ftp filefolder'      ##进入需要处理的文件所在的文件夹

for i in `ls *.txt`
do 
    if [-r "$i" ];
    then ReadTables "$i"
    else
        #dt=`cat $i | awk 'gsub(/\//,""){print}'`
        #var=$i 
        #nm_date=${var:15:22}
        dt=`cat $i | awk 'NR==3{if (gsub(/\//,"")) print $1}'`         #2018/01/08-20180108 ,“/”->“ ”
        #if [ "$nm_date" != "$dt" ]; then
        #rm -f $i
        #break 
        #fi   
        channel=`cat $i | awk 'NR==1{if (gsub(/-*(高清)?[a-z]|-/,"")) print $2; else print $2}'`   ##把“高清”和字母去掉    
        rownm=`cat $i | awk 'END{print NR}'`
        let rownm_s=${rownm}-1        ##let it be a number      
        #insert date, channel, time and program into table
        for p in `seq 5 ${rownm_s}`
        do
        tm=`cat $i | awk '{if (NR=='$p') print $1}'`
        pro=`cat $i | awk '{if (NR=='$p') print $2}'` 
        rec=0
        insert_table_sql="insert into ${TABLENAME}(date,channel,time,program,record) values('${dt}','${channel}','${tm}','${pro}','${rec}')"
        mysql -h${HOSTNAME} -u${USERNAME} -p${PASSWORD} -D ${DBNAME} -e "${insert_table_sql}" 
        done    
     fi
done
}
ReadTables ftpfile folder


#update data turn record value 0 to 1
update_sql="update ${TABLENAME} set record=1 where program in (select recordprogram from ${TABLENAME_rec}) and channel in (select recordchannel from ${TABLENAME_rec}) and time between '09:00:00' and '22:00:00'"
mysql -h${HOSTNAME} -u${USERNAME} -p${PASSWORD} -D ${DBNAME} -e "${update_sql}"

#update name1 name2 record value 0 to 1
update_sql1="update ${TABLENAME} set record=1 where channel in ('name1','name2')"

mysql -h${HOSTNAME} -u${USERNAME} -p${PASSWORD} -D ${DBNAME} -e "${update_sql1}"

注:如果需要用shell 处理txt文档,那么awk是很普遍的处理工具。 gsub(/**/,"**")是实现字符串替换的一个工具。

/ /中间是需要查找的字符串,可以使用正则表达式语法。

“ ”里面是需要替换成的内容。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值