こんにちは、2 つの物理ファイルを使用して awk を正常に動作させました。
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt
私がやろうとしていること:物理ファイルを使用するのではなく、2番目のファイルの代わりに変数を使用します..
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $variable
結果を物理ファイルにエクスポートする必要があるため、このコマンドを使用して他のファイルの値を最初のファイルにマップします...
私が試してみました
echo $variable|awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt
うまくいきませんでした:(
ここで尋ねられるように、例は次のとおりです。
cat text.txt
564 ERR0001
535 ERR0002
cat codes.txt
ERR0001 This_is_error_1
ERR0002 This_is_error_2
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt
564 ERR0001 This_is_error_1
535 ERR0002 This_is_error_2
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt text.txt |tr "_" " "
564 ERR0001 This is error 1
535 ERR0002 This is error 2
ここに失敗があります:
gg=$(cat text.txt)
echo $gg
564 ERR0001 535 ERR0002
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $gg |tr "_" " "
awk: (FILENAME=codes.txt FNR=2) fatal: cannot open file `564' for reading (No such file or directory)
IFS=' ';
echo $gg
564 ERR0001
535 ERR0002
awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt $gg |tr "_" " "
awk: (FILENAME=codes.txt FNR=2) fatal: cannot open file `564' for reading (No such file or directory)
提案されたように参加:
join -1 2 -2 1 text.txt codes.txt
ERR0001 564 This_is_error_1
ERR0002 535 This_is_error_2
join -1 2 -2 1 $gg codes.txt
join: extra operand `ERR0002'
Try `join --help' for more information.
echo $gg
564 ERR0001
535 ERR0002
三人で答えた
echo $gg|join -1 2 -2 1 - codes.txt
ERR0001 564 This_is_error_1
ERR0002 535 This_is_error_2
echo $gg|awk 'NR==FNR { _[$1]=$2 } NR!=FNR { if(_[$2] != "") print $0" "_[$2]}' codes.txt - |tr "_" " "
564 ERR0001 This is error 1
535 ERR0002 This is error 2