0

こんにちは、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
4

2 に答える 2

1

echo変数をパイプして、標準入力を読み取る任意のコマンドにパイプすることができます。

多くのUnixユーティリティは、-標準入力を意味するファイル名引数を受け入れます。

echo "$gg" | sort | join -1 2 -2 1 - text.txt

最近のバージョンのBashにも<<<"$gg"here-string演算子があります。

于 2013-02-06T16:13:11.220 に答える
0

これを行うコマンドがあります:

$ join -1 2 -2 1 text.txt codes.txt
于 2013-02-06T15:52:41.147 に答える