1

The input is a file (text.txt) containing the following lines of text (all whitespaces are space characters):

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}

From the above example I want to produce only the date / time stamp followed by all the text after the last bracket delimiter.

This is what I want from the above example:

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}

I can use cut, but it only gets stuff after the delimiter.

For example this:

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

... omits the time stamp:

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}

I can use this,

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

... but variable column locations are a problem (notice the last two lines):

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}

I have a feeling there is no way to use cut to do what I want, because I want to combine the -c option with the -d option, and I can't figure that out. Where should I go from here?

1

To answer your exact question, this is not a good fit for using cut for the following reasons:

  1. There are multiple delimiters.
  2. There may be a variable number of fields.

Using Awk:

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

Using Sed:

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

I would favor the Awk method.

  • I had been doing backflips trying to get cut to work. awk is the way to go. – Mike Galatean Oct 25 '16 at 17:01
  • @MikeGalatean, yep. :) If this solved your question, don't forget you can "accept" an answer by clicking the checkmark to the left of the answer. – Wildcard Oct 25 '16 at 17:14
0

Another sed solution:

$ 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}
  • ^([^.]+) capture all non. characters from start of line
  • .*\] ignore everything up to last ] in the line
  • (.*) capture remaining characters
  • \1\2 first and second captured groups
  • Note: Some sed versions use -r instead of -E for extended regex option
    • sed 's/^\([^.]\+\).*\]\(.*\)/\1\2/' if extended regex option is not available
0
sed 's/\(:[0-9]*\).[0-9 \-]*[a-z0-9]x[0-9a-z]*[ ]*[a-zA-Z]*[ ]*[0-9x]*[0-9 ]*/\1 /'

EDIT: the sed is using lazy pattern matching;

  • anything within brackets () is printed out in \1
  • anything else matched is disregarded
  • anything after is left untouched
  • post edited to add explination – mikejonesey Oct 25 '16 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.