如何在 Solaris 上使用 sed 删除下一行

发布于 2024-12-18 03:39:41 字数 213 浏览 4 评论 0原文

我尝试使用 sed 在文件中查找匹配模式然后删除 仅下一行。

前任。

位置
纽约
---- 删除
美国

位置
伦敦 <---- 删除
英国

我尝试过 sed '/Location/{n; d}' 可以在 linux 上运行,但不能在 solaris 上运行。

谢谢。

I trying to use sed in finding a matching pattern in a file then deleting
the next line only.

Ex.

Location
New York <---- delete
USA

Location
London <---- delete
UK

I tried sed '/Location/{n; d}' that work on linux but didn't work on solaris.

Thanks.

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

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

发布评论

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

评论(3

梦罢 2024-12-25 03:39:41

正如我在回答您的其他 sed 问题时提到的,Solaris sed 是老式的,需要更多的指导(或者换句话说),对其语法更加挑剔。

您所需要的只是一个额外的“;”字符放置在“d”字符之后,即

sed '/Location/{n; d;}'

更一般地,任何可以在 {...} 内换行的内容在汇总到单行时都需要分号分隔符。但是,您无法像在 Linux 中那样将“a”、“i”、“c”命令汇总到一行中。

在 Solaris 标准 sed 中,“a”、“i”、“c”命令需要尾随“”,后面不能有空格或制表符,\n< 上的数据数量随您喜好(可能在某个 K 限制内) /code> 终止行(没有 \r s),后跟一个空行。

较新安装的 Solaris 可能还安装了 /usr/xpg4/bin/sed。尝试一下

/usr/xpg4/bin/sed '/Location/{n; d}' 

,如果幸运的话,它将支持您的快捷语法。我无法再访问任何Solaris 机器来测试这一点。

最后,如果这不起作用,可以安装一些 GNU 工具包,其中包含一个 sed,它更像您在 Linux 中使用的工具。询问您的系统管理员 GNU 工具是否已经存在,或者是否可以安装。我不确定 gnu sed 的哪个版本开始支持“宽松”语法,因此不要假设它会在没有测试的情况下得到修复。

As I mentioned in my answer about your other sed question, Solaris sed is old-school AND needs more hand-holding (or to put it another way), is more fussy about it's syntax.

All you need is an additional ';' char placed after the `d' char, i.e.

sed '/Location/{n; d;}'

More generally, anything that can be on a new-line inside {...} needs a semi-colon separator when it is rolled up onto a single line. However, you can't roll up the 'a', 'i', 'c' commands onto a single line as you can in Linux.

In Solaris standard sed, the 'a', i', 'c' commands need a trailing '' with NO spaces or tabs after it, as much data as you like (probably within some K limit) on \n terminated lines (NO \r s), followed by a blank line.

Newer installations of Solaris may also have /usr/xpg4/bin/sed installed. Try

/usr/xpg4/bin/sed '/Location/{n; d}' 

If you're lucky, it will support your shortcut syntax. I don't have access to any solaris machines anymore to test this.

Finally, if that doesn't work, there are packages of GNU tools that can be installed that would have a sed that is much more like what you're used to from Linux. Ask your sys-admins if GNU tools are already there, or if they can be installed. I'm not sure what version of gnu sed started to support 'relaxed' syntax, so don't assume that it will be fixed without testing.

桃扇骨 2024-12-25 03:39:41

您可以将下一行附加到当前行,然后删除 Location 之外的所有内容:

$ cat text 
Location
New York <---- delete
USA

Location
London <---- delete
UK
$ sed '/Location/{N;s/Location.*$/Location/;}' text
Location
USA

Location
UK

我这里没有 Solaris,所以我想知道这是否有效。

You can append the next line to the current one and then remove everything that is not Location:

$ cat text 
Location
New York <---- delete
USA

Location
London <---- delete
UK
$ sed '/Location/{N;s/Location.*$/Location/;}' text
Location
USA

Location
UK

I do not have a Solaris here so I would like to know if this works.

从来不烧饼 2024-12-25 03:39:41

此 AWK 解决方案是否适合您 -

[jaypal~/temp]$ cat a.txt 
Location
New York <---- delete 
USA

Location
London <---- delete
UK

已更新以保留空行 -

!NF 用于保留空行。这意味着,如果字段数 = 0,则仅打印该行。 NF 是一个内置变量,用于跟踪记录中的字段数量。如果遇到空行,我们会跳过其余的处理并转到下一行。

!/Location/ 将打印这些行。这是为了保留后面没有位置的行。只要模式为真,打印就是 AWK 中的隐式操作。

第三个模式/操作是当该行与正则表达式 /Location/ 匹配时打印该行的位置。除了打印该行之外,我们还执行两次 getline ,这有效地删除了下一行,然后打印它。

[jaypal~/temp]$ awk '!NF{print;next}; !/Location/; /Location/{print;getline;getline;print}' INPUT_FILE
Location
USA

Location
UK

Does this AWK Solution works for you -

[jaypal~/temp]$ cat a.txt 
Location
New York <---- delete 
USA

Location
London <---- delete
UK

Updated to preserve the empty line -

!NF is used to preserve the blank lines. It means, if the Number of Fields is = 0 then just print the line. NF is an in-built variable which keeps track of number of fields in a record. If we encounter a blank line, we skip the rest of the processing and go to the next line.

!/Location/ will print the lines. This is to preserve the lines which are not followed by Location. Printing is an implicit action in AWK whenever the pattern is true.

The third patter/action is where we print the line when it matches the RegEx /Location/. Apart from printing the line, we do getline twice which effectively deletes your next line and then print it.

[jaypal~/temp]$ awk '!NF{print;next}; !/Location/; /Location/{print;getline;getline;print}' INPUT_FILE
Location
USA

Location
UK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文