loading...

Fish Shell: string replace

nabbisen profile image Heddi Nabbisen ・1 min read

fish shell has powerful string modules.
The subcommands are lower / upper, trim, sub (string), split, join, repeat, length, match and so on.

Thus we can replace strings via fish shell.
It's easy :)

The usage is:

$ string replace [(-a | --all)] [(-f | --filter)] [(-i | --ignore-case)] [(-r | --regex)] [(-q | --quiet)] PATTERN REPLACEMENT [STRING...]

Besides, my current fish version is 3.0.2.

Well, for example, this is how to replace a file name with one that the size information is added to:

$ string replace -r "\.jpg\$" "\-360x.jpg" "some-file-name.jpg"

With iteration, it becomes more powerful 😉

$ for x in ./*.jpg; mv "$x" (string replace -ar "\.jpg\$" "\-360x.jpg" "$x"); end

This one-liner program renames files with a regular expression pattern via string replace.

Posted on by:

nabbisen profile

Heddi Nabbisen

@nabbisen

An ICT designer/developer and a security monk. "With a cool brain and a warm heart", I am challenging unsolved problems in our society. I use OpenBSD/Rust/etc.

Discussion

pic
Editor guide