如何反转sed输出? [英] How to reverse sed output?

查看:59
本文介绍了如何反转sed输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读大约 500 个字符的行.我怎样才能得到 sed,而不是用某些东西替换该字符串,而是用某些东西替换该行的其余部分?简而言之,我想删除指定字符串周围的所有文本.用 awk 删除列是行不通的,因为在匹配的字符串前后有不确定的字符数.有什么想法吗?

I'm reading from a line that's about 500 characters. How can I get sed to, instead of replacing that string with something, replace the rest of the line with something? In short, I want to remove all the text around a specified string. Deleting columns with awk won't work, because there is an indeterminate amount of characters before and after the matched string. Any ideas?

推荐答案

尝试使用反向引用:

sed 's/.*\(searchstring\).*/___\1___/'

搜索字符串周围的 .* 将匹配除字符串之外的所有内容,括号告诉 sed 记住它匹配的内容.您可以使用 \1 引用第一个匹配的字符串.

The .*'s around the search string will match everything but the string, and the parentheses tell sed to remember what it matched. You can refer to the first matched string with \1.

这是一个示例(替换除bar baz"之外的所有内容):

Here's an example (replacing everything but 'bar baz'):

$ echo "foo bar baz qux" | sed 's/.*\(bar baz\).*/___\1___/'
___bar baz___

你可以用上面你喜欢的任何模式替换'bar baz';为简单起见,我只使用了一个基本字符串.

You can replace 'bar baz' with whatever pattern you like in the above; I just used a basic string for simplicity.

这篇关于如何反转sed输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆