题
我一直在尝试实现一个从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中绊倒。
解决方案
如果您想根据这些行的内容从文件中选择整行, grep
可能是最合适的工具。但是,某些字符(如星号)对 grep
具有特殊含义,因此需要“转义”。用反斜杠。这将只打印以四颗星和一个空格开头的行:
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