0

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

示例文件转储:

**** 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

4 回答 4

4

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

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

但是,您想保留匹配的行,因此您需要执行此操作的-v选项grep:打印与模式匹配的行。

grep -v "\*\*\*\* " textfile

那应该给你你想要的。

于 2009-10-24T10:15:15.343 回答
2
sed '/^\*\{4\} .* \*\{4\}$/d'

或者稍微宽松一点

sed '/^*\{4\}/d'
于 2009-10-24T10:27:22.373 回答
1
 sed 's/^*.*//g' test | grep .
于 2009-10-24T10:22:14.657 回答
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"
于 2009-10-24T12:21:09.863 回答