删除文本文件中的多行
我一直在尝试实现一个从 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 中让我绊倒。
I've been trying to implement a bash script that reads from wordnet's online database and have been wondering if there is a way to remove a variety text files with one command.
Example 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"
I just need to remove the lines which describe aspects of grammar e.g.
**** Noun ****
**** Verb ****
**** Adjective ****
So that I have a clean file with only definitions of the words:
(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"
The * symbols around the grammatical terms are tripping me up in sed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想仅根据文件中的内容选择整行,
grep
可能是最合适的工具。但是,某些字符(例如星号)对于grep
具有特殊含义,因此需要使用反斜杠“转义”。这将只打印以四颗星和一个空格开头的行:但是,您希望保留与此不匹配的行,因此您需要 -v 选项for
grep
的作用就是:打印与模式不匹配的行。那应该会给你你想要的。
If you want to select whole lines from a file based just on the content of those lines,
grep
is probably the most suitable tool available. However, some characters, such as your stars, have special meanings togrep
, so need to be "escaped" with a backslash. This will print just the lines starting with four stars and a space:However, you want to keep the lines which don't match that, so you need the
-v
option forgrep
which does just that: prints the lines which don't match the pattern.That should give you what you want.
或者宽松一点
or a bit looser