0

How can I capitalize letters before a certain character? (^)

I am trying to do the reverse of the above link.

I want to capitalise everything after a certain character or word.

It can be using awk, sed or bash

example

before

 foo^bar
 foobar ^ foobar

after

 foo^BAR
 foobar ^ FOOBAR

Thanks

2 Answers 2

1

With sed:

sed 's/\^.*/\U&/' file

(^ is escaped, to remove its special meaning as the start-of-line anchor; you could also use [^])

or awk

awk -F^ 'BEGIN{OFS=FS} {x=$1; $0=toupper($0); $1=x} 1' file

(store the first ^-separated field; change the whole line to upper case; then replace the original first field).

1
  • Much apprecited, I have been trying all day to get this right.
    – yolo_techy
    Feb 1, 2022 at 14:13
1

With plain bash, we need a couple of extra variables:

for line in 'foo^bar' 'foobar ^ foobar' 'a^b^c'; do
  prefix=${line%%^*}
  suffix=${line#*^}
  caps="${prefix}^${suffix^^}"
  printf '%s ==> %s\n' "$line" "$caps"
done
foo^bar ==> foo^BAR
foobar ^ foobar ==> foobar ^ FOOBAR
a^b^c ==> a^B^C
  • ${var%%pattern} -> remove from the end the longest substring matching the pattern
  • ${var#pattern} -> remove from the beginning the shortest substring matching the pattern
  • ${var^^} -> capitalize

Ref: 3.5.3 Shell Parameter Expansion

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .