awk命令中的substring

时间:2014-03-31 12:19:44

标签: awk substring

我正在使用awk命令:

df -Pk | grep /var/prod/data | awk '{q=sprintf("%c", 39); print "INSERT INTO entity_trend(entity_name,entity_type,run_date,count) VALUES (\47DISKSPACE\47,\47SPACE\47,SYSDATE,"$5");";}' > cmd.sql

因此,cmd.sql的内容将是:

INSERT INTO entity_trend(entity_name,entity_type,run_date,count) VALUES ('DISKSPACE','SPACE',SYSDATE,**52%**);

相反,我想:

INSERT INTO entity_trend(entity_name,entity_type,run_date,count) VALUES ('DISKSPACE','SPACE',SYSDATE,**52**);

我应该如何形成substring命令?

1 个答案:

答案 0 :(得分:1)

添加+0以摆脱%$5+0

awk '{q=sprintf("%c", 39); print "INSERT... SYSDATE,"$5+0");";}' > cmd.sql
                                                       ^^

查看示例:

$ echo "55%" | awk '{print $0}'
55%
$ echo "55%" | awk '{print $0+0}'
55