Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalise a varying phrase in same position in Terminal

Tags:

text

bash

sed

awk

perl

I have a number of .py files that contain a line like this:

self._cols = [self.foo.bar.relevant_string.str()]

I need to capitalise the relevant_string, to get this

self._cols = [self.foo.bar.RELEVANT_STRING.str()]

in all ~100 of them.

Is there a way to do this with bash/awk/perl? I tried something like this

perl  -pe 's/self.foo.bar./uc($&)/e' *.py

But it capitalised the captured area, not the part after.

like image 680
Josh Friedlander Avatar asked Dec 09 '25 09:12

Josh Friedlander


1 Answers

With GNU sed:

$ echo 'self._cols = [self.foo.bar.relevant_string.str()]' |
    sed -E 's/(self\.foo\.bar\.)([^.]+)/\1\U\2/'
self._cols = [self.foo.bar.RELEVANT_STRING.str()]
like image 115
Ed Morton Avatar answered Dec 10 '25 23:12

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!