I am writing a function in zsh
which has a string
"find . -a -print:10"
the :10
part needs to be trimmed off the right. This might change in the future to become :23
or :77
etc so it seems a pattern like :[0-9]+
is needed.
also, the string might have the form
find . -a -print
In which case if there is no :[0-9]+
pattern on the end, then the string should be left unchanged.
so
find . -a -print:10
should becomefind . -a -print
andfind . -a -print
should stay asfind . -a -print
What I have tried so far
% sed -nr 's/(.*)(:[0-9]+)?/\1/p' <<<'find . -a -print:10'
find . -a -print:10 # ':10' not getting trimmed
If I try
sed -nr 's/(.*)(:[0-9]+)/\1/p' <<<'find . -a -print:10'
find . -a -print # ':10' getting trimmed GOOD ✔✔
but the same sed
expression
sed -nr 's/(.*)(:[0-9]+)/\1/p' <<<'find . -a -print'
# no output
How can I right trim this string?
"${str%:*}"
)? – steeldriver Oct 04 '16 at 01:27sed
doesn't kill any lines unless you tell it to. – Wildcard Oct 04 '16 at 01:30*
globs match too many characters, i.e. if the string becamefind -iname "foo\:bar.txt"
then the glob '' in `"${str%:}"would match
bar.txt. Im sure
zsh` has some fancier globbing (extended globs) but I'm not experienced with them. – the_velour_fog Oct 04 '16 at 01:31string
matches the regex:[0-9]+$
(and if it does updatestring
value to${string%:*}
) – don_crissti Oct 04 '16 at 10:40