0

I need to convert something like this

androidx.arch.core.executor.AppToolkitTaskExecutor

To this

Androidx.Arch.Core.Executor.AppToolkitTaskExecutor

I know that sed could likely help me but I'm not proficient enough to build the correct command.

StackOverflower
  • 5,463
  • 13
  • 58
  • 89
  • 1
    What was your research on that? I found similar: [Capitalize words using `sed` or `awk`](https://stackoverflow.com/questions/1538676/uppercasing-first-letter-of-words-using-sed) Could adjust for `.` boundaries, idea? – hc_dev Feb 24 '21 at 00:33

3 Answers3

5

Pure Bash solution:

#!/usr/bin/env bash

class=androidx.arch.core.executor.AppToolkitTaskExecutor

IFS=.

# Split Fully Qualified class path elements into array using dot delimiter
read -r -a class_array <<<"$class"

# Join the class_array's capitalized class path elements using the dot delimiter
printf -v title_class '%s' "${class_array[*]^}"

# Printout the original Fully Qualified Class Name
# and its matching Title Case Class Name
printf 'FQCN: %s\nTCCN: %s\n' "$class" "$title_class"

Output:

FQCN: androidx.arch.core.executor.AppToolkitTaskExecutor
TCCN: Androidx.Arch.Core.Executor.AppToolkitTaskExecutor

Alternate method with function:

#!/usr/bin/env bash

class2Title () {
  IFS=.
  set -- $@
  echo "${*^}"
}

class2Title net.example.bar.Foo

Output:

Net.Example.Bar.Foo
Léa Gris
  • 17,497
  • 4
  • 32
  • 41
2

⚠️ Not tested!

# Converts to upper-case:
# first word-letter at start or following a period 
$ sed -e 's/^.|\.\w+/\u&/g' input.txt > output.txt

This uses common regular expression matcher:

  • ^ start of line, followed by any single character . (i.e. the initial letter at start)
  • | as boolean OR
  • \. for the period, followed by \w+ for one or many word-characters (letters)

but also some specials like:

  • back-reference & (full match pattern), where following is applied to:
  • transformation from GNU extensions \u (uppercase the next character of match), that may not work on MacOS.

Use sed's GNU extensions on MacOS

On MacOS (BSD sed) you can use gsed which must be installed, e.g. using homebrew:

brew install gnu-sed

Inspired by: https://stackoverflow.com/a/4581564

Alternatively you may achieve same (regex-uppercase transformation) by using Perl: https://unix.stackexchange.com/a/615804

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • 2
    Done, used `perl -Mopen=locale -pe 's/^.|\../\U$&/g'`. Will mark your solution since helped me to get to the correct regex. Thanks!! – StackOverflower Feb 24 '21 at 01:45
0

This is the fully functioning sed statement. (tested)

find ./ -type f -readable -writable -exec sed -i "s/androidx.arch.core.executor.AppToolkitTaskExecutor/Androidx.Arch.Core.Executor.AppToolkitTaskExecutor/g" {} \;

Note that in the find I left it recursively from the directory you are currently "in" .. If you need to search a specific directory, it'll look like find /path/to/your/dir -type ...

Zak
  • 6,976
  • 2
  • 26
  • 48