Oleg
Oleg

Reputation: 13

How to remove all but first character before dot

Suppose you have a string

anyString.ofSomeSort...

and you always need to trim it to

a.ofSomeSort...

so just keep the first character of the substring before the first dot.

Any ideas how a regex for this could look like?

Upvotes: 0

Views: 646

Answers (1)

Benjamin W.
Benjamin W.

Reputation: 52162

You could use sed:

$ sed 's/\(.\)[^.]*/\1/' <<< 'anyString.ofSomeSort...'
a.ofSomeSort...

This captures the first character, then drops everything that's not a period after that first character.

Or with shell parameter expansion:

$ str='anyString.ofSomeSort...'
$ echo "${str:0:1}${str#${str%%.*}}"
a.ofSomeSort...

${str:0:1} is just the first character, and ${str#${str%%.*}} retains everything from the first period on.

Upvotes: 1

Related Questions