使用awk添加常量值

问题描述 投票:1回答:1

如果value小于240000,我需要向第4列添加常量值。常数值是010000。我写了命令,但它没有给出任何输出。以下是示例数据和脚本。请帮帮我。谢谢。

命令:

awk  '{
  if($4 -lt 240000)
    $4= $4+010000;
 }' Test.txt

样本数据 :

1039,1018,20180915,000000,0,0,A
1039,1018,20180915,010000,0,0,A
1039,1018,20180915,020000,0,0,A
1039,1018,20180915,030000,0,0,A
1039,1018,20180915,240000,0,0,A
1039,1018,20180915,050000,0,0,A
1039,1018,20180915,060000,0,0,A
1039,1018,20180915,070000,1,0,A
1039,1018,20180915,080000,0,1,A
1039,1018,20180915,090000,2,0,A
1039,1018,20180915,241000,0,0,A
1039,1018,20180915,240500,0,0,A 
shell unix awk scripting
1个回答
2
投票
$ awk  '
BEGIN { FS=OFS="," }                 # input and output field separators
{
    if($4<240000)                    # if comparison
        $4=sprintf("%06d",$4+10000)  # I assume 10000 not 010000, also zeropadded to 6 chars
        # $4+=10000                  # if zeropadding is not required
    print                            # output
 }' file

输出:

1039,1018,20180915,010000,0,0,A
1039,1018,20180915,020000,0,0,A
1039,1018,20180915,030000,0,0,A
1039,1018,20180915,040000,0,0,A
1039,1018,20180915,240000,0,0,A
1039,1018,20180915,060000,0,0,A
1039,1018,20180915,070000,0,0,A
1039,1018,20180915,080000,1,0,A
1039,1018,20180915,090000,0,1,A
1039,1018,20180915,100000,2,0,A
1039,1018,20180915,241000,0,0,A
1039,1018,20180915,240500,0,0,A

qazxsw poi not qazxsw poi,因为qazxsw poi输出qazxsw poi,因为它是该值的八进制表示。

© www.soinside.com 2019 - 2024. All rights reserved.