【问题标题】:How to avoid generating intermediate files in bash script如何避免在 bash 脚本中生成中间文件
【发布时间】:2017-07-07 22:07:23
【问题描述】:

我想知道是否可以更改以下脚本,以便不生成“intermediate.tmp”作为输出:

在命令行调用脚本:

./script.sh file1 file2

script.sh:

#!/bin/bash

FILE_1=$1
FILE_2=$2

awk '{print $1,$2}' $FILE_1 > intermediate.tmp

awk 'NR==FNR {h[$1] = $0; next} {print $0,h[$1]}' intermediate.tmp $FILE_2 > output.file

awk 脚本本身并不重要。我只想知道如何将intermediate.tmp“输入”到第二个awk命令中,而不需要生成除了所需的output.file之外的intermediate.tmp输出文件。

谢谢。

【问题讨论】:

  • 脚本的目的是什么?

标签: bash awk output


【解决方案1】:
awk 'NR==FNR {h[$1] = $1 OFS $2; next} {print $0,h[$1]}' "$FILE_1" "$FILE_2" > output.file

或不太明智:

awk '{print $1,$2}' "$FILE_1" |
awk 'NR==FNR {h[$1] = $0; next} {print $0,h[$1]}' - "$FILE_2" > output.file

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 2020-11-03
    • 2017-02-06
    • 2017-03-11
    相关资源
    最近更新 更多