Retorna as primeiras colunas, omite tudo depois dele até um delimitador e adiciona o resto?

1

A entrada é um arquivo (text.txt) contendo as seguintes linhas de texto (todos os espaços em branco são caracteres de espaço):

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}

No exemplo acima, desejo produzir somente o carimbo de data / hora seguido por todo o texto após o último delimitador de colchetes.

Isso é o que eu quero do exemplo acima:

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}

Eu posso usar o cut , mas ele só pega coisas depois do delimitador.

Por exemplo, isto:

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

... omite o carimbo de hora:

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}

Eu posso usar isso,

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

... mas os locais das colunas variáveis são um problema (observe as duas últimas linhas):

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}

Tenho a sensação de que não há como usar cut para fazer o que eu quero, porque quero combinar a opção -c com a opção -d, e não consigo descobrir . Para onde devo ir daqui?

    
por Mike Galatean 25.10.2016 / 02:08

3 respostas

1

Para responder à sua pergunta exata, isso não é uma boa opção para usar cut pelos seguintes motivos:

  1. Existem vários delimitadores.
  2. Pode haver um número variável de campos.

Usando o Awk:

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

Usando Sed:

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

Eu preferiria o método Awk.

    
por 25.10.2016 / 02:34
0

Outra solução sed :

$ sed -E 's/^([^.]+).*\](.*)//' 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}
  • ^([^.]+) captura todos os caracteres não . do início da linha
  • .*\] ignora tudo até o último ] na linha
  • (.*) captura os caracteres restantes
  • primeiro e segundo grupos capturados
  • Observação: algumas sed versões usam -r em vez de -E para a opção regex estendida
    • sed 's/^\([^.]\+\).*\]\(.*\)//' se a opção regex estendida não estiver disponível
por 25.10.2016 / 03:21
0
sed 's/\(:[0-9]*\).[0-9 \-]*[a-z0-9]x[0-9a-z]*[ ]*[a-zA-Z]*[ ]*[0-9x]*[0-9 ]*/ /'

EDIT: o sed está usando correspondência de padrões preguiçosos;

  • qualquer coisa entre colchetes () é impressa em \ 1
  • qualquer outra coisa correspondida é desconsiderada
  • qualquer coisa depois é deixada intocada
por 25.10.2016 / 02:37