Freewind
Freewind

Reputation: 198348

Is there any string manipulation commands/libraries I can use in bash/fishshell?

I'm a newbie of bash/fish, and found some string operations are quite difficult, like:

  1. toUpperCase/toLowerCase
  2. a string is starts/ends with another string
  3. a string contains another string
  4. get the suffix of a file name
  5. trim a string
  6. check if matched a regular expression

and so on. Although I can find all kinds of solutions for most of the cases, but I find it not easy to remember or use.

So I just wonder if there is any command/library which supports most of the common string operations, suppose its name is mystr, so I can just use it like this:

  1. mystr --to-upper somestring
  2. mystr --starts-with sss somestring
  3. mystr --contains bbb sometring
  4. mystr --suffix somestring
  5. mystr --trim somestring
  6. mystr --match "some.*" somestring
  7. mystr --find-match "some(.*)" somestring $1, I mean just get the matched part in (.*)

Upvotes: 3

Views: 149

Answers (1)

ridiculous_fish
ridiculous_fish

Reputation: 18561

It's not in a release, but top-of-tree fish has a nice new strings feature: https://github.com/fish-shell/fish-shell/issues/156

Other than that, you would generally use a mishmash of external commands. This is Unix, after all!

  1. Uppercase: echo something | tr "[:lower:]" "[:upper:]"
  2. Prefix: echo stuff | grep -q '^stu'
  3. Contains: echo stuff | grep -q 'tuf'
  4. Suffix: echo stuff | grep -q 'uff$'
  5. Trim spaces: echo ' hello ' | sed 's/^[[:space:]]*//g' | sed 's/[[:space:]]*$//g'
  6. Regex match: echo somestring | grep -q '^some.*$'
  7. Regex search: echo somestring | grep -q 'some.*'

Upvotes: 3

Related Questions