다음 출력을 얻으려고합니다. 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