从文件中删除行

发布于 2024-12-15 11:24:35 字数 823 浏览 3 评论 0原文

我有一个与此类似的问题: 使用 SED 或 AWK 从文件中删除行

如何删除以 3 和 5 开头的行以及此文件中的所有内容:

ssadfsadfsdf
asdfsadf
asdfsadf
asdfsadf
asdfsdf
1 z z z lkj klj lkj
2 satan er kaerleikur lkj lkj lkj
3 z z z
4 asdflj asd sdf
5 z z z
6 asdf sdaf asdf
7 z z z lkj lkj lkj
8 sdf safd asdf
9 z z z

如果答案明确在其他地方,我将删除此问题。

如果它看起来像这样怎么办:

asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        3 z z z
asd        4 asdflj asd sdf
asd        5 z z z
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z

I have a question similar to this one:
Delete lines from file with SED or AWK

how can I delete rows that start with 3 and 5 and everything in between in this file:

ssadfsadfsdf
asdfsadf
asdfsadf
asdfsadf
asdfsdf
1 z z z lkj klj lkj
2 satan er kaerleikur lkj lkj lkj
3 z z z
4 asdflj asd sdf
5 z z z
6 asdf sdaf asdf
7 z z z lkj lkj lkj
8 sdf safd asdf
9 z z z

If the answer is clearly elsewhere I will delete this question.

what if it looked liked this:

asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        3 z z z
asd        4 asdflj asd sdf
asd        5 z z z
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

櫻之舞 2024-12-22 11:24:35

您可以使用 awk 范围模式 跳过匹配模式 1 到模式 2 的行:

awk '$1 == 3, $1 == 5 { next }; { print }' INFILE

我对字段 $1 进行数值比较,而不是使用正则表达式,例如 /^3/ 因为它也会匹配“30”、“31”等。

如果数字是该行的第二个字段(如您编辑的示例输入中所示),只需更改 $1 到$2。让我知道这是否适合您。

You can use an awk range pattern to skip line matching pattern 1 up to pattern 2:

awk '$1 == 3, $1 == 5 { next }; { print }' INFILE

I do a numerical comparison on field $1 instead of a regular expression such as /^3/ because that would also match '30', '31', etc.

If the number is the second field of the line (as in your edited example input), just change $1 to $2. Let me know if this works for you.

网白 2024-12-22 11:24:35

谷歌很快给了我这个答案:

http://www. cyberciti.biz/faq/sed-howto-remove-lines-paragraphs/

$ sed '/WORD1/,/WORD2/d' input.txt > output.txt

要确保它与您所说的“行开头”匹配,您可以使用 ^ 开始该模式 象征。

如果您使用 Vim(内置 sed),您可以执行 range 命令,例如:

:/^3.*/,/^5.*/ d

其中 d 是删除命令,:/exp1/,/exp2/ 范围。

A quick Google gave me this as seconds answer:

http://www.cyberciti.biz/faq/sed-howto-remove-lines-paragraphs/

$ sed '/WORD1/,/WORD2/d' input.txt > output.txt

To make sure it's matching "beginning of line" like you said, you can start the pattern with the ^ symbol.

If you're using Vim (which has sed built in) you can do a range command like:

:/^3.*/,/^5.*/ d

where d is the delete command, and :/exp1/,/exp2/ the range.

﹏半生如梦愿梦如真 2024-12-22 11:24:35
awk '/^3/ {del=1}
     (del==0) {print}
     /^5/ {del=0}' FILE
awk '/^3/ {del=1}
     (del==0) {print}
     /^5/ {del=0}' FILE
时光与爱终年不遇 2024-12-22 11:24:35
awk '$2!~/^[3|5]/' yourFile 

会为你做的。

请参阅下面的测试:

kent$  echo "asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        3 z z z
asd        4 asdflj asd sdf
asd        5 z z z
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z"|awk '$2!~/^[3|5]/'

输出:

asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        4 asdflj asd sdf
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z
awk '$2!~/^[3|5]/' yourFile 

will do it for you.

see the test below:

kent$  echo "asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        3 z z z
asd        4 asdflj asd sdf
asd        5 z z z
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z"|awk '$2!~/^[3|5]/'

output:

asd        ssadfsadfsdf
asd        asdfsadf
asd        asdfsadf
asd        asdfsadf
asd        asdfsdf
asd        1 z z z lkj klj lkj
asd        2 satan er kaerleikur lkj lkj lkj
asd        4 asdflj asd sdf
asd        6 asdf sdaf asdf
asd        7 z z z lkj lkj lkj
asd        8 sdf safd asdf
asd        9 z z z
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文