입력은 다음 텍스트 줄을 포함하는 파일(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
귀하의 정확한 질문에 답변하자면 다음과 같은 이유로 사용하기에 적합하지 않습니다 .
- 여러 개의 구분 기호가 있습니다.
- 필드 수는 가변적일 수 있습니다.
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로 인쇄됩니다.
- 다른 일치 항목은 무시됩니다.
- 그 이후의 내용은 변함이 없습니다