Score:0

How can I capitalise letters after a certain character

eg flag

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

Score:1
cn flag

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

Score:1
hr flag

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

eg flag
Much apprecited, I have been trying all day to get this right.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.