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- 

...しかし、可変列の位置が問題となります (最後の 2 行に注意してください)。

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捕獲された第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で印刷されます
  • 他に一致したものは無視されます
  • それ以降は何も変更されません

関連情報