Remover uma variedade de linhas em um arquivo de texto
Pergunta
Eu tenho tentado implementar um script que lê a partir do banco de dados on-line da wordnet e foram perguntando se existe uma maneira de remover um arquivos de texto variedade com um comando.
Exemplo filedump:
**** Noun ****
(n)hello, hullo, hi, howdy, how-do-you-do (an expression of greeting) "every morning they exchanged polite hellos"
**** Verb ****
(v)run (move fast by using one's feet, with one foot off the ground at any given time) "Don't run--you'll be out of breath"; "The children ran to the store"
**** Adjective ****
(adj)running ((of fluids) moving or issuing in a stream) "as mountain stream with freely running water"; "hovels without running water"
Eu só preciso remover as linhas que descrevem aspectos da gramática, por exemplo
**** Noun ****
**** Verb ****
**** Adjective ****
Assim que eu tenho um arquivo limpo com apenas definições das palavras:
(n)hello, hullo, hi, howdy, how-do-you-do (an expression of greeting) "every morning they exchanged polite hellos"
(v)run (move fast by using one's feet, with one foot off the ground at any given time) "Don't run--you'll be out of breath"; "The children ran to the store"
(adj)running ((of fluids) moving or issuing in a stream) "as mountain stream with freely running water"; "hovels without running water"
Os símbolos * em torno dos termos gramaticais estão me tropeçar em sed.
Solução
Se você quiser selecionar linhas inteiras de um arquivo baseado apenas no conteúdo dessas linhas, grep
é provavelmente a ferramenta mais adequada disponível. No entanto, alguns personagens, como suas estrelas, têm significado especial para grep
, por isso necessidade de ser "escapado" com uma barra invertida. Isto irá imprimir apenas as linhas que começam com quatro estrelas e um espaço:
grep "^\*\*\*\* " textfile
No entanto, você quer manter as linhas que não corresponder, então você precisa a opção -v
para grep
que faz exatamente isso: imprime as linhas que não correspondem ao padrão.
grep -v "\*\*\*\* " textfile
Isso deve lhe dar o que você quer.
Outras dicas
sed '/^\*\{4\} .* \*\{4\}$/d'
ou um pouco mais solto
sed '/^*\{4\}/d'
sed 's/^*.*//g' test | grep .
# awk '!/^\*\*+/' file
(n)hello, hullo, hi, howdy, how-do-you-do (an expression of greeting) "every morning they exchanged polite hellos"
(v)run (move fast by using one's feet, with one foot off the ground at any given time) "Don't run--you'll be out of breath"; "The children ran to the store"
(adj)running ((of fluids) moving or issuing in a stream) "as mountain stream with freely running water"; "hovels without running water"