You are not logged in.

#1 2014-09-08 21:21:58

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,686
Website

Bash parameter expansion... how can I remove two substrings? [SOLVED]

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 packagesZsh and other configs

Offline

#2 2014-09-08 21:25:36

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,414

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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

#3 2014-09-08 21:26:22

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,686
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

@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 packagesZsh and other configs

Offline

#4 2014-09-08 21:36:03

ninian
Member
From: United Kingdom
Registered: 2008-02-24
Posts: 726
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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

#5 2014-09-08 21:38:48

Saint0fCloud
Member
Registered: 2009-03-31
Posts: 137

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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 hmm

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

#6 2014-09-08 21:38:55

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,686
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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 packagesZsh and other configs

Offline

#7 2014-09-08 21:40:39

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

You can't nest parameter expansions in bash (you can in zsh: `${${myvar%.*}#.*/}`).


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2014-09-08 22:15:53

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,686
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

@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 packagesZsh and other configs

Offline

#9 2014-09-08 22:27:06

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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

#10 2014-09-09 05:26:35

aiBo
Member
Registered: 2010-11-10
Posts: 50

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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

#11 2014-10-01 22:27:11

nomorewindows
Member
Registered: 2010-04-03
Posts: 3,414

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

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

#12 2014-10-01 23:02:18

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Bash parameter expansion... how can I remove two substrings? [SOLVED]

nomorewindows: please don't emptybump, especially on this board https://wiki.archlinux.org/index.php/Fo … mpty_Posts


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB