删除文本文件中的各种行

时间:2009-10-24 10:10:10

标签: linux sed awk grep cat

我一直在尝试实现一个从wordnet的在线数据库读取的bash脚本,并且一直想知道是否有办法用一个命令删除各种文本文件。

示例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中绊倒。

4 个答案:

答案 0 :(得分:4)

如果您想根据这些行的内容从文件中选择整行,grep可能是最合适的工具。但是,某些字符(例如您的星星)对grep具有特殊含义,因此需要使用反斜杠“转义”。这将只打印以四颗星和一个空格开头的行:

grep "^\*\*\*\* " textfile

但是,您希望保留匹配的行,因此您需要-v的{​​{1}}选项才能执行此操作:打印行匹配模式。

grep

那可以给你你想要的东西。

答案 1 :(得分:2)

sed '/^\*\{4\} .* \*\{4\}$/d'

或者有点宽松

sed '/^*\{4\}/d'

答案 2 :(得分:1)

 sed 's/^*.*//g' test | grep .

答案 3 :(得分:0)

# 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"
相关问题