Awk 사용:

Awk 사용:

입력은 다음 텍스트 줄을 포함하는 파일(text.txt)입니다(모든 공백은 공백 문자임).

2016-10-24 10:25:48.939279-0400 0x63a55    Info        0x0                  1416   backupd: (TimeMachine) [com.apple.TimeMachine.TMLogInfo] Found 2735 files (298.6 MB) needing backup
2016-10-24 10:25:48.954707-0400 0x63a55    Info        0x0                  1416   backupd: (TimeMachine) [com.apple.TimeMachine.TMLogInfo] 6.08 GB required (including padding), 1.2 TB available
2016-10-24 10:27:56.721350-0400 0x63a55    Info        0x0                  1416   backupd: (TimeMachine) [com.apple.TimeMachine.TMLogInfo] Copied 3128 items (283.1 MB) from volume Macintosh HD. Linked 5756.
2016-10-24 10:27:59.652854-0400 0x63a55    Info        0x0                  1416   backupd: (TimeMachine) [com.apple.TimeMachine.TMLogInfo] Created new backup: 2016-10-24-102758
2016-10-24 10:27:59.638560-0400 0x64abb    Error       0x0                  52     UserEventAgent: (TimeMachine) [com.apple.TimeMachine.TMLogError] Failed to send message because the port couldn't be created.
2016-10-24 10:28:00.545654-0400 0x63a55    Error       0x0                  1416   backupd: (TimeMachine) [com.apple.TimeMachine.TMLogError] Could not back up OS X Recovery to /Volumes/BackupA/Backups.backupdb: Error Domain=NSCocoaErrorDomain Code=-69830 "Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk" UserInfo={NSLocalizedDescription=Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk}

위의 예에서 나는 생산하고 싶습니다오직날짜/시간 스탬프 뒤에 마지막 대괄호 구분 기호 뒤의 모든 텍스트가 옵니다.

위의 예에서 내가 원하는 것은 다음과 같습니다.

2016-10-24 10:25:48 Found 2735 files (298.6 MB) needing backup
2016-10-24 10:25:48 6.08 GB required (including padding), 1.2 TB available
2016-10-24 10:27:56 Copied 3128 items (283.1 MB) from volume Macintosh HD. Linked 5756.
2016-10-24 10:27:59 Created new backup: 2016-10-24-102758
2016-10-24 10:27:59 Failed to send message because the port couldn't be created.
2016-10-24 10:28:00 Could not back up OS X Recovery to /Volumes/BackupA/Backups.backupdb: Error Domain=NSCocoaErrorDomain Code=-69830 "Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk" UserInfo={NSLocalizedDescription=Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk}

나는 사용할 수 있다자르다, 그러나 구분 기호 뒤에 있는 내용만 가져옵니다.

예를 들면 다음과 같습니다.

cat ~/Desktop/test.txt | grep TimeMachine | rev | cut -d']' -f1 | rev

... 타임스탬프를 생략합니다.

Found 2735 files (298.6 MB) needing backup
6.08 GB required (including padding), 1.2 TB available
Copied 3128 items (283.1 MB) from volume Macintosh HD. Linked 5756.
Created new backup: 2016-10-24-102758
Failed to send message because the port couldn't be created.
Could not back up OS X Recovery to /Volumes/BackupA/Backups.backupdb: Error Domain=NSCocoaErrorDomain Code=-69830 "Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk" UserInfo={NSLocalizedDescription=Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk}

이것을 사용할 수 있는데,

cat ~/Desktop/test.txt | grep TimeMachine | cut -c 1-19,140- 

...하지만 가변 열 위치가 문제입니다(마지막 두 줄 참조).

2016-10-24 10:25:48 Found 2735 files (298.6 MB) needing backup
2016-10-24 10:25:48 6.08 GB required (including padding), 1.2 TB available
2016-10-24 10:27:56 Copied 3128 items (283.1 MB) from volume Macintosh HD. Linked 5756.
2016-10-24 10:27:59 Created new backup: 2016-10-24-102758
2016-10-24 10:27:59ogError] Failed to send message because the port couldn't be created.
2016-10-24 10:28:00] Could not back up OS X Recovery to /Volumes/BackupA/Backups.backupdb: Error Domain=NSCocoaErrorDomain Code=-69830 "Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk" UserInfo={NSLocalizedDescription=Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk}

사용할 일이 없을 것 같은 느낌이 들어요자르다-c 옵션과 -d 옵션을 결합하려고 하는데 그것을 알 수 없기 때문에 원하는 것을 수행합니다. 여기서 어디로 가야 하나요?

답변1

cut정확한 질문에 대답하자면 다음과 같은 이유로 사용하기에 적합하지 않습니다 .

  1. 여러 개의 구분 기호가 있습니다.
  2. 필드 수는 다양할 수 있습니다.

Awk 사용:

awk -F']' '{print substr($0,1,19), $NF}' text.txt

Sed 사용:

sed 's/^\(....-..-.. ..:..:..\).*\]\([^]]*\)$/\1 \2/' text.txt

나는 Awk 방법을 선호합니다.

답변2

또 다른 sed해결책:

$ sed -E 's/^([^.]+).*\](.*)/\1\2/' ip.txt 
2016-10-24 10:25:48 Found 2735 files (298.6 MB) needing backup
2016-10-24 10:25:48 6.08 GB required (including padding), 1.2 TB available
2016-10-24 10:27:56 Copied 3128 items (283.1 MB) from volume Macintosh HD. Linked 5756.
2016-10-24 10:27:59 Created new backup: 2016-10-24-102758
2016-10-24 10:27:59 Failed to send message because the port couldn't be created.
2016-10-24 10:28:00 Could not back up OS X Recovery to /Volumes/BackupA/Backups.backupdb: Error Domain=NSCocoaErrorDomain Code=-69830 "Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk" UserInfo={NSLocalizedDescription=Failed to get info for unmounted Recovery partition (error -69830): This operation requires an unmounted disk}
  • ^([^.]+).줄 시작 부분에서 문자 가 아닌 모든 것을 캡처합니다.
  • .*\]]줄의 마지막까지 모두 무시
  • (.*)남은 문자 캡처
  • \1\2첫 번째 및 두 번째 캡처된 그룹
  • 참고: 일부 sed버전에서는 확장 정규식 옵션 -r대신 사용됩니다.-E
    • sed 's/^\([^.]\+\).*\]\(.*\)/\1\2/'확장 정규식 옵션을 사용할 수 없는 경우

답변3

sed 's/\(:[0-9]*\).[0-9 \-]*[a-z0-9]x[0-9a-z]*[ ]*[a-zA-Z]*[ ]*[0-9x]*[0-9 ]*/\1 /'

편집: sed는 게으른 패턴 일치를 사용하고 있습니다.

  • 괄호() 안의 내용은 \1에 인쇄됩니다.
  • 일치하는 다른 항목은 무시됩니다.
  • 이후의 모든 것은 그대로 유지됩니다

관련 정보