简体   繁体   English

sed / regex:如何匹配字符串中的“<”或“>”

[英]sed/regex: How to match a '<' or '>' in a string

I'm looking to match all less than ('<') or greater than ('>') signs in a file using sed. 我希望使用sed匹配文件中所有小于('<')或大于('>')的符号。 I only want to match the single character 我只想匹配单个字符

My goal is to replace them with ' <' and '> ' (ensure they have white space around them so I can parse them easier) respectively. 我的目标是用' <''> '替换它们(确保它们周围有空白区域,以便我可以更容易地解析它们)。

For example, it would match: (without space within the tags) 例如,它将匹配:(标签内没有空格)

< p >Hey this is a paragraph.< /p >< p >And here is another.< /p >

.. and turn it into (note the spaces) ..并把它变成(注意空格)

 < p > Hey this is a paragraph. < /p >  < p > And here is another. < /p > 



Here's what my initial (wrong) guess was: 这是我最初的(错误的)猜测:

sed 's/<{1}|>{1}/ <> /' ...


It matches the whole word/line, which is not desired, and it also does not replace correctly. 它匹配整个单词/行,这是不需要的,它也不能正确替换。

Anyways, any help would be appreciated! 无论如何,任何帮助将不胜感激! Thanks! 谢谢!

尝试两次替换以使其更容易:

sed 's/</ </g ; s/>/> /g' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM