简体   繁体   English

如何使用 sed 将以“/”开头的单词的每一行中的最后一个实例大写?

[英]How do I use sed to capitalize the last instance in each line of a word that starts with “/”?

I'm trying to capitalize every word that starts with "/" while ignoring the same word that doesn't start with "/" using sed.我正在尝试使用 sed 将每个以“/”开头的单词大写,同时忽略不以“/”开头的同一个单词。 So for example, if the output has例如,如果输出有

word1
/word1

I want to leave the first word1 untouched and capitalize the second word1 to become我想保持第一个 word1 不变并将第二个 word1 大写成为

word1
/WORD1

If the file has multiple word1 in the same line, I want it to capitalize the last instance of word1 so如果文件在同一行中有多个 word1,我希望它大写 word1 的最后一个实例

word1 /word1 /word1

would become会成为

word1 /word1 /WORD1

I have tried sed 's/word1/\\U&/' but it simply capitalizes the every word1 in the file and I'm not quite sure how to include "/" in the search string as it would just throw an error if I add it straight on.我试过sed 's/word1/\\U&/'但它只是将文件中的每个 word1 大写,我不太确定如何在搜索字符串中包含“/”,因为如果我添加它只会引发错误它直接。

https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html https://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html

"The / characters may be uniformly replaced by any other single character within any given s command. The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \\ character. " “在任何给定的 s 命令中,/ 字符可以被任何其他单个字符统一替换。/ 字符(或任何其他代替它的字符)只能出现在正则表达式或替换中,前提是它前面有一个 \\ 字符。 ”

https://www.gnu.org/software/sed/manual/html_node/Regexp-Addresses.html#Regexp-Addresses "/regexp/ https://www.gnu.org/software/sed/manual/html_node/Regexp-Addresses.html#Regexp-Addresses "/regexp/

This will select any line which matches the regular expression regexp. If regexp itself includes any / characters, each must be escaped by a backslash (\). "

I don't got any experience using sed, but it seems to me as you can use regex to concretely specify patterns which you want to get capitalized.我没有任何使用 sed 的经验,但在我看来,因为您可以使用正则表达式来具体指定您想要大写的模式。 In the regex expressions you can use the character "/", but you need to escape it with a backslash.在正则表达式中,您可以使用字符“/”,但需要使用反斜杠对其进行转义。 So try something like所以尝试类似的东西

sed 's/word1/\\U&/' (I'm not quite sure where the backslash needs to be put, but by this and trying around you should be able to give the regular expression of your word with the escaped "/" before and only this word should be capitalized. sed 's/word1/\\U&/'(我不太确定需要将反斜杠放在哪里,但是通过这个和尝试,您应该能够在之前使用转义的“/”给出单词的正则表达式并且只有这个词应该大写。

As can be seen at https://ideone.com/htALOt --https://ideone.com/htALOt可以看出——

sed -Ee 's@(.*)(/word1)@\1\U\2@'

The (.*) matches as much as possible from the beginning, which is reproduced unchanged when \\1 is encountered. (.*)从头开始尽可能匹配,当遇到\\1时会原样复制。 \\U tells GNU sed to start making replacements in all-uppercase, and \\2 then writes that content in all-caps. \\U告诉 GNU sed开始以全大写形式进行替换,然后\\2以全大写形式写入该内容。

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

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