增加并增加特定列的值

时间:2018-02-20 10:00:41

标签: bash awk

我有一些文件,我希望每次循环读取新文件时添加第3列(+ 2000)中的值,并输出第3列中已完成更改的新文件

这是我尝试做的事情

if (Get-Item 'yourfile.html'| Where-Object Length -gt 5kb) {
      # do something
}

这里是所需的输入和输出。

INPUT1

  for tt in file file2 file3
    do
    awk '{$3=sprintf("2%06s",$3)} 1'  $tt > file1a
    done

OUTPUT1

X  9367      211L  36829.00  47705.001 7561 77761  37260.00  47706.00  48136.001
X  9367     1101L  36829.00  47717.001 7771 79921  37260.00  47706.00  48148.001
X  9367     1201L  36829.00  47729.001 7981 82081  37260.00  47706.00  48160.001
X  9367     2491L  36829.00  47741.001 8191 84241  37260.00  47706.00  48172.001

输入2

X  9367    20211L  36829.00  47705.001 7561 77761  37260.00  47706.00  48136.001
X  9367    21101L  36829.00  47717.001 7771 79921  37260.00  47706.00  48148.001
X  9367    21201L  36829.00  47729.001 7981 82081  37260.00  47706.00  48160.001
X  9367    22491L  36829.00  47741.001 8191 84241  37260.00  47706.00  48172.001

OUTPUT2

X  8367      211L  36829.00  47705.001 7561 77761  37260.00  47706.00  48136.001
X  8367     1101L  36829.00  47717.001 7771 79921  37260.00  47706.00  48148.001
X  8367     1201L  36829.00  47729.001 7981 82081  37260.00  47706.00  48160.001
X  8367     2491L  36829.00  47741.001 8191 84241  37260.00  47706.00  48172.001

输入3

X  8367    40211L  36829.00  47705.001 7561 77761  37260.00  47706.00  48136.001
X  8367    41101L  36829.00  47717.001 7771 79921  37260.00  47706.00  48148.001
X  8367    41201L  36829.00  47729.001 7981 82081  37260.00  47706.00  48160.001
X  8367    42491L  36829.00  47741.001 8191 84241  37260.00  47706.00  48172.001

OUTPUT3

X  7367      211L  36829.00  47705.001 7561 77761  37260.00  47706.00  48136.001
X  7367     1101L  36829.00  47717.001 7771 79921  37260.00  47706.00  48148.001
X  7367     1201L  36829.00  47729.001 7981 82081  37260.00  47706.00  48160.001
X  7367     2491L  36829.00  47741.001 8191 84241  37260.00  47706.00  48172.001

1 个答案:

答案 0 :(得分:1)

Awk 解决方案:

awk 'FNR==1{ c+=2 }{ $3=sprintf("%d%04d", c, $3); print > FILENAME"_output" }' file1 file2 file3

结果:

$ head file[123]_output
==> file1_output <==
X 9367 20211 36829.00 47705.001 7561 77761 37260.00 47706.00 48136.001
X 9367 21101 36829.00 47717.001 7771 79921 37260.00 47706.00 48148.001
X 9367 21201 36829.00 47729.001 7981 82081 37260.00 47706.00 48160.001
X 9367 22491 36829.00 47741.001 8191 84241 37260.00 47706.00 48172.001

==> file2_output <==
X 8367 40211 36829.00 47705.001 7561 77761 37260.00 47706.00 48136.001
X 8367 41101 36829.00 47717.001 7771 79921 37260.00 47706.00 48148.001
X 8367 41201 36829.00 47729.001 7981 82081 37260.00 47706.00 48160.001
X 8367 42491 36829.00 47741.001 8191 84241 37260.00 47706.00 48172.001

==> file3_output <==
X 7367 60211 36829.00 47705.001 7561 77761 37260.00 47706.00 48136.001
X 7367 61101 36829.00 47717.001 7771 79921 37260.00 47706.00 48148.001
X 7367 61201 36829.00 47729.001 7981 82081 37260.00 47706.00 48160.001
X 7367 62491 36829.00 47741.001 8191 84241 37260.00 47706.00 48172.001

另一种方式是bash循环:

for f in file[123]; do
    c=$((c+2))
    awk -v c=$c '{ $3=sprintf("%d%04d", c, $3) }1' "$f" > "${f}_output"
done