删除文本文件中的多行

发布于 2024-08-08 23:58:40 字数 1183 浏览 5 评论 0原文

我一直在尝试实现一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-08-15 23:58:40

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

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

但是,您希望保留与此不匹配的行,因此您需要 -v 选项for grep 的作用就是:打印与模式不匹配的行。

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

那应该会给你你想要的。

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 to grep, so need to be "escaped" with a backslash. This will print just the lines starting with four stars and a space:

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

However, you want to keep the lines which don't match that, so you need the -v option for grep which does just that: prints the lines which don't match the pattern.

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

That should give you what you want.

揽清风入怀 2024-08-15 23:58:40
sed '/^\*\{4\} .* \*\{4\}$/d'

或者宽松一点

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

or a bit looser

sed '/^*\{4\}/d'
满意归宿 2024-08-15 23:58:40
 sed 's/^*.*//g' test | grep .
 sed 's/^*.*//g' test | grep .
白衬杉格子梦 2024-08-15 23:58:40
# 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"
# 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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文