テキストファイルのさまざまな行を削除する
質問
wordnetのオンラインデータベースから読み取るbashスクリプトを実装しようとしており、1つのコマンドでさまざまなテキストファイルを削除する方法があるかどうか疑問に思っていました。
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"
単に文法の側面を説明する行を削除する必要があります。
**** Noun ****
**** Verb ****
**** Adjective ****
つまり、単語の定義のみを含むクリーンなファイルを作成します:
(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"
文法用語の周りの*記号は、sedで私をつまずかせます。
解決
それらの行の内容のみに基づいてファイルから行全体を選択する場合、おそらく grep
が利用可能な最も適切なツールです。ただし、星などの一部の文字は grep
に対して特別な意味を持つため、「エスケープ」する必要があります。バックスラッシュ付き。これにより、4つの星とスペースで始まる行のみが印刷されます。
grep "^\*\*\*\* " textfile
ただし、一致しない 行は保持する必要があるため、 grep
には -v
オプションが必要です。それだけ:パターンに一致しない 行を出力します。
grep -v "\*\*\*\* " textfile
それはあなたが望むものを与えるはずです。
他のヒント
sed '/^\*\{4\} .* \*\{4\}$/d'
または少し緩い
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"
所属していません StackOverflow