AWK脚本将数据复制到文件 [英] awk in script to copy data to a file

查看:131
本文介绍了AWK脚本将数据复制到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我抄了一些列的数据的一些文件,然后试图一列数据写入到另一个文件。但我得到一些错误

这是我的输入文件: -

 ,E2Bn9,2015-04-29 00:00:00-0500['2C173'],E2BA8,2015-04-29 00:00:00-0500['5A475','2C174'],E2BA8,2015-06-29 00:00:00-0400

我用 AWK SED 命令如下:

  SED -i的/,/ | / g的临时文件
awk的-F'[|,]''{打印更新表设置可乐=$ 1,其中COLB =$ 2和COLC =$ 3}'临时文件> updatestmt.cql

我得到的输出

 更新表设置可乐=其中COLB = E2Bn9和COLC = 2015年4月29日00:00:00-0500更新表中设置的可乐= ['2C173']其中COLB = E2BA8和COLC = 2015年4月29日00:00:00-0500更新表中设置的可乐=[5A475,其中COLB ='2C174']和COLC = E2BA8

1日两行似乎不错,但最后一行是印刷错误的值。

我想最后一排为

 更新表设置可乐=[5A475,2C174'],其中COLB = E2BA8和COLC = 2015-07-02 00:00:00-0400


解决方案

通过GNU AWK 4 *为 FPAT

  $ AWK -v FPAT =([^] *)|([[] [^] + [])''{打印更新表中设置可乐= ,$ 1,,其中COLB =,$ 2和COLC =,$ 3}文件
更新表中设置可乐=其中COLB = E2Bn9和COLC = 2015年4月29日00:00:00-0500
更新表中设置的可乐= ['2C173']其中COLB = E2BA8和COLC = 2015年4月29日00:00:00-0500
更新表中设置的可乐= ['5A475','2C174']其中COLB = E2BA8和COLC = 2015-07-02 00:00:00-0400

请参阅 http://www.gnu.org /software/gawk/manual/gawk.html#Splitting-By-Content

对于非GAWK awks或pre-4.0版本的gawk(获得现代GAWK!)你可以使用:

  $猫tst.awk
{
    删除˚F
    NF = 0
    尾= $ 0个
    而((尾=)及与放大器;匹配(尾,/([^,] *)|([[] [^] + []])/)!){
        F [++ NF = SUBSTR(尾,RSTART,RLENGTH)
        尾= SUBSTR(尾,RSTART + RLENGTH + 1)
    }
    打印更新表中设置的可乐=中,f [1],其中COLB =中,f [2],和COLC =中,f [3]
}$ AWK -f tst.awk文件
更新表中设置可乐=其中COLB = E2Bn9和COLC = 2015年4月29日00:00:00-0500
更新表中设置的可乐= ['2C173']其中COLB = E2BA8和COLC = 2015年4月29日00:00:00-0500
更新表中设置的可乐= ['5A475','2C174']其中COLB = E2BA8和COLC = 2015-07-02 00:00:00-0400

您可以使用 $ 1,0 而不是的F [] 但后来有一个性能开销作为记录得到重新拆分每次分配给 $(++ NF),并要使用原来可能存在的情况下时间 $ 1,0 更高版本。

I have copied some columns data to some file and then tried to write one column data to another file. But am getting few wrong

This is my input file:-

,E2Bn9,2015-04-29 00:00:00-0500

['2C173'],E2BA8,2015-04-29 00:00:00-0500

['5A475','2C174'],E2BA8,2015-06-29 00:00:00-0400

I used the awk, sed commands as follows

sed -i 's/",/|/g' tempFile
awk -F '[|,]' '{ print "update table set cola = " $1 " where colb = " $2 " and colc = " $3 }' tempFile > updatestmt.cql

I got the output as

update table set cola = where colb = E2Bn9 and colc = 2015-04-29 00:00:00-0500

update table set cola = ['2C173'] where colb = E2BA8 and colc = 2015-04-29 00:00:00-0500

update table set cola = "['5A475' where colb =  '2C174'] and colc = E2BA8

1st two rows seems fine but last row it is printing wrong value.

I want the last row as

update table set cola = "['5A475','2C174'] where colb =E2BA8 and colc = 2015-06-29 00:00:00-0400

解决方案

With GNU awk 4.* for FPAT:

$ awk -v FPAT='([^,]*)|([[][^]]+[]])' '{print "update table set cola =", $1, "where colb =", $2, "and colc =", $3}' file
update table set cola =  where colb = E2Bn9 and colc = 2015-04-29 00:00:00-0500
update table set cola = ['2C173'] where colb = E2BA8 and colc = 2015-04-29 00:00:00-0500
update table set cola = ['5A475','2C174'] where colb = E2BA8 and colc = 2015-06-29 00:00:00-0400

See http://www.gnu.org/software/gawk/manual/gawk.html#Splitting-By-Content.

With non-gawk awks or pre-4.0 versions of gawk (get a modern gawk!) you can use:

$ cat tst.awk
{
    delete f
    nf = 0
    tail = $0
    while ( (tail!="") && match(tail,/([^,]*)|([[][^]]+[]])/) ) {
        f[++nf] = substr(tail,RSTART,RLENGTH)
        tail = substr(tail,RSTART+RLENGTH+1)
    }
    print "update table set cola =", f[1], "where colb =", f[2], "and colc =", f[3]
}

$ awk -f tst.awk file
update table set cola =  where colb = E2Bn9 and colc = 2015-04-29 00:00:00-0500
update table set cola = ['2C173'] where colb = E2BA8 and colc = 2015-04-29 00:00:00-0500
update table set cola = ['5A475','2C174'] where colb = E2BA8 and colc = 2015-06-29 00:00:00-0400

You can use $0 instead of f[] but then there's a performance overhead as the record gets re-split every time you assign to $(++nf) and there may be cases where you want to use the original $0 later.

这篇关于AWK脚本将数据复制到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆