Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash last index of

Sorry for the lame bash question, but I can't seem to be able to work it out.

I have the following simple case:

  • I have variable like artifact-1.2.3.zip

  • I would like to get a sub-string between the hyphen and the last index of the dot (both exclusive).

My bash skill are not too strong. I have the following:

a="artifact-1.2.3.zip"; b="-"; echo ${a:$(( $(expr index "$a" "$b" + 1) - $(expr length "$b") ))}

Producing:

1.2.3.zip

How do I remove the .zip part as well?

like image 610
carlspring Avatar asked Apr 22 '13 17:04

carlspring


People also ask

How do I get the last index of an array in bash?

In Bash how can one get the last index of an array? This works: $((${#array[@]} - 1)) but is not very pretty. To get the last element of an array one can do ${myarray[-1]} is there a similar option for index? declare -i index; index=${#array[@]}-1; echo $index ?

How do I get the last character of a string in bash?

To access the last character of a string, we can use the parameter expansion syntax ${string: -1} in the Bash shell. In bash the negative indices count from the end of a string, so -1 is the index of a last character. Note: Space is required after the colon (:); otherwise it doesn't work.

How do you find the last occurrence of a character in a string in Linux?

The strrchr() function finds the last occurrence of c (converted to a character) in string . The ending null character is considered part of the string .

How do I split a string in bash?

In bash, a string can also be divided without using $IFS variable. The 'readarray' command with -d option is used to split the string data. The -d option is applied to define the separator character in the command like $IFS. Moreover, the bash loop is used to print the string in split form.


1 Answers

$ a="artifact-1.2.3.zip"; a="${a#*-}"; echo "${a%.*}"

#pattern’ removes pattern so long as it matches the beginning of $a. The syntax of pattern is similar to that used in filename matching. In our case,

  • * is any sequence of characters.
  • - means a literal dash.
  • Thus #*- matches everything up to, and including, the first dash.
  • Thus ${a#*-} expands to whatever $a would expand to, except that artifact- is removed from the expansion, leaving us with 1.2.3.zip.

Similarly, ‘%pattern’ removes pattern so long as it matches the end of the expansion. In our case,

  • . a literal dot.
  • * any sequence of characters.
  • Thus %.* is everything including the last dot up to the end of the string.
  • Thus if $a expands to 1.2.3.zip, then ${a%.*} expands to 1.2.3.

Job done.

The man page content for this is as follows (at least on my machine, YMMV):

       ${parameter#word}
       ${parameter##word}
              The word is expanded to produce a pattern just  as  in  pathname
              expansion.  If the pattern matches the beginning of the value of
              parameter, then the result of  the  expansion  is  the  expanded
              value of parameter with the shortest matching pattern (the ``#''
              case) or the longest matching pattern (the ``##'' case) deleted.
              If parameter is @ or *, the pattern removal operation is applied
              to each positional parameter in turn, and the expansion  is  the
              resultant  list.   If parameter is an array variable subscripted
              with @ or *, the pattern removal operation is  applied  to  each
              member  of the array in turn, and the expansion is the resultant
              list.

       ${parameter%word}
       ${parameter%%word}
              The word is expanded to produce a pattern just  as  in  pathname
              expansion.   If  the  pattern  matches a trailing portion of the
              expanded value of parameter, then the result of the expansion is
              the  expanded value of parameter with the shortest matching pat-
              tern (the ``%'' case)  or  the  longest  matching  pattern  (the
              ``%%''  case)  deleted.   If  parameter  is  @ or *, the pattern
              removal operation is applied to  each  positional  parameter  in
              turn,  and the expansion is the resultant list.  If parameter is
              an array variable subscripted with @ or *, the  pattern  removal
              operation  is  applied  to each member of the array in turn, and
              the expansion is the resultant list.

HTH!

EDIT

Kudos to @x4d for the detailed answer. Still think people should RTFM though. If they don't understand the manual, then post another question.

like image 175
bobbogo Avatar answered Sep 28 '22 15:09

bobbogo