gpt4 book ai didi

linux - 替换字符串中的空格并转换为小写

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:49 24 4
gpt4 key购买 nike

我有多个 html 文件,我需要替换空格并使字符串在字符串中小写。 (全部在Linux中)

Exaple:
<html> ....
<a href="bla.com/CCC C C">ddd ddd ddd</a>
<a href="bla.com/CCC C">ddd ddd ddd</a>
...
</html>

Should result in:
<html> ....
<a href="bla.com/ccc_c_c">ddd ddd ddd</a>
<a href="bla.com/ccc_c">ddd ddd ddd</a>
...
</html>

页面上还有其他类似的链接,但它们不是 bla.com,而是其他链接,因此如果使用常规 exp(bla.com 需要在那里)。CCC部分不是静态的,可以是任何单词!

有什么单行句可以做到这一点吗?

最佳答案

I need to replace a space and make strings lower case within a string

对于只有一个单个空白符,那么这个衬垫就可以做到这一点:

sed -E 's/(bla.com\/)(\w*)\s*(.*?")/\1\L\2_\L\3/g' file
<小时/>
$ echo '<a href="bla.com/CCC C">ddd ddd ddd</a>' | sed -E 's/(bla.com\/)(\w*)\s*(.*?")/\1\L\2_\L\3/g'
<a href="bla.com/ccc_c">ddd ddd ddd</a>

说明:

s/            # Substitution
(bla.com\/) # Match the domain (captured)
(\w*) # Match the following word (captured)
\s* # Followed by whitespace
(.*?") # Capture everything left upto the closing "
/ # Replace with
\1 # The captured domain
\L\2 # Lowercase first captured word
_ # Replace the whitespace with an underscore
\L\3 # Lowercase rest of the match
/g # Global

如果像您的示例中可能有多个空格,我很难想出一个单行。

关于linux - 替换字符串中的空格并转换为小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13709035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com