输入是一个包含以下文本行的文件 (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 中打印出来
- 任何其他匹配的内容都会被忽略
- 之后的任何内容都保持不变