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メソッドを好みます。

おすすめ記事