You are not logged in.
I want to take the output of a find command and:
1) Remove the preceding ./
2) Remove the file extension
Example:
myvar=./running_with_scissors.txt
echo ${myvar%.*}
./running_with_scissors
echo ${myvar#./}
running_with_scissors.txt
I need to combine the two so that my echo commend returns simply: running_with_scissors
Is this possible without defining a 2nd var?
Last edited by graysky (2014-09-08 22:16:11)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
Wouldn't this be a job for sed/awk/etc. ?
I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.
Offline
@nomorewindows - I can do it with sed but I want to learn how to use shell expansion if possible.
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
I think you need two assignments:
myvar=./running_with_scissors.txt; myvar=${myvar%.*}; myvar=${myvar#.*/}; echo "$myvar"
Or am I missing something?!
Edit: yup, you want to retain the original value of $myvar - sorry!
Last edited by ninian (2014-09-08 21:37:33)
Offline
If you're using zsh, this will work, although it's not very elegant or flexible.
$ a="./hello.txt"
$ print ${a[(ws./.)2]%.*}
hello
Edit: I got so excited about the problem that I failed to read the thread title properly
For future posterity, a much better zsh solution (in addition to nesting the substitutions) would be:
$ setopt hist_subst_pattern
$ print ${myvar:t:s/.*//}
Last edited by Saint0fCloud (2014-09-08 22:16:20)
Offline
Perhaps it cannot be done (googling suggests this is the case). Again, I can do it with a temp var but we'll see what others come up with.
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
You can't nest parameter expansions in bash (you can in zsh: `${${myvar%.*}#.*/}`).
Offline
@jason - Nice! Thanks for the tip. I use zsh as my default anyway.
myvar=./running_with_scissors.txt
% echo ${${myvar%.*}#.*/}
running_with_scissors
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
For bashers:
echo ${myvar:2:${#myvar}-6}
Though this assumes the "./" is always there, and the extension is always 3 characters.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
If Trilby's assumptions apply, the follwing code works as well in bash:
myvar=./running_with_scissors.txt
echo ${myvar:2:-4} # => running_with_scissors
Or, if you don't mind some regular expression:
myvar=./running_with_scissors.txt
[[ $myvar =~ ^\./(.*)\.[^.]+$ ]]
echo ${BASH_REMATCH[1]} # => running_with_scissors
Last edited by aiBo (2014-09-09 05:46:00)
Offline
Didn't anyone ever tell you not to run with scissors?
I may have to CONSOLE you about your usage of ridiculously easy graphical interfaces...
Look ma, no mouse.
Offline
nomorewindows: please don't emptybump, especially on this board https://wiki.archlinux.org/index.php/Fo … mpty_Posts
Offline