0

I'm trying to capitalize every word that starts with "/" while ignoring the same word that doesn't start with "/" using 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

If the file has multiple word1 in the same line, I want it to capitalize the last instance of word1 so

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1234 5678
  • 23
  • 4
  • `s@/word1@\U&@` -- you aren't restricted to `/` as the only possible sigil; it's just a common choice. – Charles Duffy Jul 16 '21 at 16:44
  • This seems to work for the example above but if theres three occurence of word1 in the same line, it capitalizes the second word1 and ignores the third one with "/" in front of it. how would I use the command to capitalize the last occurance of word1 in every line? – 1234 5678 Jul 16 '21 at 17:20
  • _Only_ the last, or _every_ instance _including_ the last? If you want _every_ instance, add a `g` after the final sigil (after the `@` in my first comment). And since the body of the question doesn't indicate that you want to replace only the last instance, be sure you edit to that effect. – Charles Duffy Jul 16 '21 at 17:34
  • Sorry about that, I only need the last instance of word1 of each line to be capitalized. I'll edit the body of the question right away, sorry again! – 1234 5678 Jul 16 '21 at 17:36
  • Okay. And by "every word", do you mean "every instance of the specific word word1", or "every alphabetic word", or something else? – Charles Duffy Jul 16 '21 at 17:39
  • if the file has only one line, and the line has multiple occurances of the word "word1", then I only want the last occurance of "word1" to be capitalized. So if the file has one line, and that line is word1 word1 /word1, I want it to become word1 word1 /WORD1 – 1234 5678 Jul 16 '21 at 17:42
  • That's not what I asked. I asked if `word2` should also be replaced. "Every word" doesn't mean "every instance of word1", it means, well, _every word_. (That said, afk for lunch, so if my answer does need to be changed it'll be later). – Charles Duffy Jul 16 '21 at 17:44
  • No, I only need "word1" to be changed. The other words can be left alone. – 1234 5678 Jul 16 '21 at 17:45

2 Answers2

0

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. "

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. 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.

Janosch
  • 84
  • 1
  • 6
0

As can be seen at 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. \U tells GNU sed to start making replacements in all-uppercase, and \2 then writes that content in all-caps.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • If this solves your problem, clicking the checkbox to the left of an answer will mark that answer as having addressed the problem (and will change the formatting of the question itself in the homepage/list to mark it resolved). – Charles Duffy Jul 16 '21 at 18:20