WonderCsabo
WonderCsabo

Reputation: 12207

Sed uppercase lines if they starting with an uppercase character

I want the lines starting with one uppercase character to be uppercased, other lines should be not touched.

So this input:

cat myfile
a
b
Cc

should result in this output:

a
b
CC

I tried this command, but this not matches if i use grouping:

cat myfile | sed -r 's/\([A-Z]+.*\)/\U\1/g'

What am i doing wrong?

Upvotes: 1

Views: 246

Answers (5)

mklement0
mklement0

Reputation: 438238

\U for uppercase conversion is a GNU sed extension.

Alternative for platforms where that is not available (e.g., macOS, with its BSD awk implementation):

awk '/^[A-Z]/ { print toupper($0); next } 1'

Upvotes: 1

potong
potong

Reputation: 58440

This might work for you (GNU sed):

sed 's/^[[:upper:]].*/\U&/' file

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed '/^[A-Z].*[a-z]/ s/.*/\U\1/' YourFile

only on line that are not compliant

Upvotes: 0

lurker
lurker

Reputation: 58254

cat myfile | sed 's/^\([A-Z].*\)$/\U\1/'

Upvotes: 1

Barmar
Barmar

Reputation: 781255

When you use the -r option, you must not put \ before parentheses used for grouping. So it should be:

sed -r 's/^([A-Z].*)/\U\1/' myfile

Also, notice that you need ^ to match the beginning of the line. The g modifier isn't needed, since you're matching the entire line.

Upvotes: 4

Related Questions