awk 将错误的变量打印到文件
awk prints wrong variable out to file
Objective
我试图将字段 2 中显示的日期分解为一个变量 "year and "month",然后将其作为 2 个新字段打印回我的文件中。我有一个 for 循环,因为那里我正在对很多文件执行此操作,最后将对这些文件进行分类。
问题
AWK 在文件末尾为其中一个变量打印字段 3,为另一个变量打印空白。
文件 - test.tsv
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288
代码
##note the tabs in the cut command are correct in my code but might end up as spaces in the post
for f4 in $(find *.tsv);do
name=$(echo $f4 | sed -Ee 's/-filename.tsv//')
month=$(cat $f4 | cut -d " " -f 2 | awk -F"-" -v OFS='-' '{print }')
year=$(cat $f4 | cut -d " " -f 2 | awk -F"-" -v OFS='-' '{print }')
awk -v month="$month" -v year="$year" -F"\t" -v OFS='\t' '{print [=11=], $month, $year}' $f4 > $name-dates.tsv
mv $f4 tsv
done
当前输出
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 platform
期望的输出
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 03 2020
我试过的
许多谷歌和脚本的大量修改,但我似乎无法正确处理。根据我的变量 "month" 和 "year" 我正在捕获正确的输入,但 awk 命令中的某些内容没有正确传递它。任何指导将不胜感激。
调用 awk 一次即可完成:
$ awk 'BEGIN{FS=OFS="\t"} {split(,d,/-/); print [=10=], d[2], d[1]}' test.tsv
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 03 2020
另请参阅 why-is-looping-over-finds-output-bad-practice, https://mywiki.wooledge.org/Quotes, and http://porkmail.org/era/unix/award.html for some (but not all) of the other other issues in your script. I highly recommend you run all scripts you write through shellcheck
(e.g. https://www.shellcheck.net/),直到您了解基础知识。
Objective
我试图将字段 2 中显示的日期分解为一个变量 "year and "month",然后将其作为 2 个新字段打印回我的文件中。我有一个 for 循环,因为那里我正在对很多文件执行此操作,最后将对这些文件进行分类。
问题
AWK 在文件末尾为其中一个变量打印字段 3,为另一个变量打印空白。
文件 - test.tsv
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288
代码
##note the tabs in the cut command are correct in my code but might end up as spaces in the post
for f4 in $(find *.tsv);do
name=$(echo $f4 | sed -Ee 's/-filename.tsv//')
month=$(cat $f4 | cut -d " " -f 2 | awk -F"-" -v OFS='-' '{print }')
year=$(cat $f4 | cut -d " " -f 2 | awk -F"-" -v OFS='-' '{print }')
awk -v month="$month" -v year="$year" -F"\t" -v OFS='\t' '{print [=11=], $month, $year}' $f4 > $name-dates.tsv
mv $f4 tsv
done
当前输出
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 platform
期望的输出
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 03 2020
我试过的
许多谷歌和脚本的大量修改,但我似乎无法正确处理。根据我的变量 "month" 和 "year" 我正在捕获正确的输入,但 awk 命令中的某些内容没有正确传递它。任何指导将不胜感激。
调用 awk 一次即可完成:
$ awk 'BEGIN{FS=OFS="\t"} {split(,d,/-/); print [=10=], d[2], d[1]}' test.tsv
client 2020-03-15 platform 3096431 2686357 0.868 2544716 0.8220000000000001 2509205 0.8100000000000001 2046915 0.6609999999999999 0.189 0.053 0.0526 0 0.0001 0 0.013 0.009599999999999999 0 0.0031 0 0 0.0005999999999999999 0.37 0.0757 0 0.0365 0.2326 0.025 1238347 0.494 0.494 1270858 0.507 0 0 1 0.302 33 202.5 1270864 0.507 157387 0.063 357414 0.142 723540 0.288 03 2020
另请参阅 why-is-looping-over-finds-output-bad-practice, https://mywiki.wooledge.org/Quotes, and http://porkmail.org/era/unix/award.html for some (but not all) of the other other issues in your script. I highly recommend you run all scripts you write through shellcheck
(e.g. https://www.shellcheck.net/),直到您了解基础知识。