次の出力を取得しようとしています。 2つの入力ファイルがあり、2つの入力ファイルとその下の.name/of/file行に共通の:nameが必要です。
私が今まで試したこと:
awk 'FNR==NR { a[$1]; next }NF<=1{ flag=0 }$1 in a { print; flag=1; delete a[$1]; next }flag{ printf "%s\n",$0 }' file1 file2
出力:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
入力ファイル1:
:name1
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name2
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name1
./name/of/file [logfile] [ error in file coming since Day : 40]
./name/of/file [logfile] [ error in file coming since Day : 40 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
:name5
./name/of/file [logfile] [ error in file coming since Day : 6 ]
./name/of/file [logfile] [ error in file coming since Day : 6 ]
:name4
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
./name/of/file [logfile] [ error in file coming since Day : 10 ]
入力ファイル2:
:name1 ABC123 12345
:name3 EFG789 67898
:name4 HIJ547 01234
必須出力ファイル:
:name1 ABC123 12345
./name/of/file [logfile] [ error in file coming since Day : 1 ]
./name/of/file [logfile] [ error in file coming since Day : 1 ]
:name3 EFG789 67898
./name/of/file [logfile] [ error in file coming since Day : 24 ]
./name/of/file [logfile] [ error in file coming since Day : 24 ]
:name4 HIJ547 01234
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
./name/of/file [logfile] [ error in file coming since Day : 3 ]
答え1
- コマンドにタイプミスがあります:とを置き換える必要があり
file1
ますfile2
。 - 最初のフィールドは配列にindexとして格納されますが、
a
残りの行(または$2
sums$3
)は値として失われます。この値は後で印刷する必要があります。 - 予想される出力に一致する4つのインデントスペースがありません。
古い:
awk '
FNR==NR { a[$1]; next }
NF<=1{ flag=0 }
$1 in a { print; flag=1; delete a[$1]; next }
flag{ printf "%s\n",$0 }
' file1 file2
新しい:
awk '
FNR==NR { k=$1; $1=""; a[k]=$0; next }
NF<=1{ flag=0 }
$1 in a { print $0 a[$1]; flag=1; delete a[$1]; next }
flag{ print " " $0 }
' file2 file1