당신의 모든 것이 잘 되길 바랍니다!
이것이 내가 달성하고 싶은 것입니다:
다음 줄이 있습니다.
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully
내부의 값을 얻고 싶습니다.인덱스 이름"WRITING"과 일치하는 줄에 대괄호를 추가하고 "Sent bytes"와 일치하는 줄 끝에 추가합니다.바이트브래킷 일치.
예를 들어 원하는 출력은 다음과 같습니다.
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully 20220601_cfgsys_017507001635
대괄호 안의 2개 값(바이트 및 인덱스 이름)을 가져오는 방법을 알고 있습니다.
cat test.txt | grep "WRITING" | awk -F"[()]" '{print $2" "$4}'
$2 = 괄호 안의 바이트 값 $4 = 괄호 안의 인덱스 이름 값
하지만 필수 행에 추가할 수는 없습니다. 다음 행("WRITING" 뒤)에서 "Sent bytes" 문자열을 검색해야 하며 $2는 "Sent bytes" 행의 $2와 같고 $4를 추가해야 합니다. "이미 바이트 보내기" 줄 끝으로
어떤 도움이라도 대단히 감사하겠습니다!
미리 감사드립니다!
답변1
awk를 사용하십시오.
$ cat tst.awk
BEGIN { FS = "[() ]+" }
($3 == "WRITING") && ($7 == "indexname") { idx[$5] = $8 }
(($3" "$4) == "Sent bytes") && ($5 in idx) { $0 = $0 OFS idx[$5] }
{ print }
$ awk -f tst.awk file
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes (2566) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes (4301) successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes (1293) successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes (7285) successfully 20220601_cfgsys_017507001635
답변2
사용awk
$ awk -F"[()]" '/indexname/{var=$4}/Sent bytes/{$(NF+1)=var}1' input_file
2022-10-19 09:12:39.174 WRITING bytes(2566), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.334 WRITING bytes(4301), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.334 Sent bytes 2566 successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.334 WRITING bytes(1293), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:39.398 Sent bytes 4301 successfully 20220701_cfgsys_017507001635
2022-10-19 09:12:39.398 WRITING bytes(7285), indexname(20220601_cfgsys_017507001635)
2022-10-19 09:12:39.890 Sent bytes 1293 successfully 20220601_cfgsys_017507001635
2022-10-19 09:12:39.890 WRITING bytes(4289), indexname(20220701_cfgsys_017507001635)
2022-10-19 09:12:40.123 Sent bytes 7285 successfully 20220701_cfgsys_017507001635