I am running a shell script and I have the following string:
keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12" How do I fetch string before keystore: i.e /mnt/blumeta0/db2. I know how to strip on a single character delimiter and the path before keystore can change. I tried:
arrIN=(${keystore_location//\"keystore\"/ })
You want
arrIN=${keystore_location%%keystore*}
echo $arrIn
/mnt/blumeta0/db2/
The %% operator removes the longest match, reading from the right side of the string
Note that there are also operators
% --- remove first match from the right side of the string
# --- remove first match starting from the left side of the string
## --- remove longest match starting for the left side of the string.
IHTH
$ keystore_location="/mnt/blumeta0/db2/keystore/keystore.p12"
$ echo "${keystore_location%%/keystore*}"
/mnt/blumeta0/db2
%%/keystore* removes the longest suffix matching /keystore* -which is a glob pattern- from $keystore_location.
Related
I got string as followed:
text=\"abcdef\"gfijk\"lmno\"
How can I extract the text between last two " - so I will get only lmno ?
I tried to use & but without success
sub_text=${text&\"*}
echo ${sub_text&\"*}
This is easily done with parameter expansion.
First, delete that final quote with ${var%pattern}, which removes the shortest match for pattern from the end of $var:
result=${text%'"'} # result=\"abcdef\"gfijk\"lmno
Then, delete everything from the beginning up to the last remaining quote with ${var##pattern}, which removes the longest match for pattern from the beginning of $var:
result=${result##*'"'} # result=lmno
...and then you're there:
echo "$result"
Given String:->
approximateLastUseTime: '2019-10-15T16:56:07.082500Z'
I need to extract everything after the T and before the Z (after the colon)
With bash, you can use substring operator, or prefix/suffix
T=2019-10-15T16:56:07.082500Z
# Using substring
echo ${T:11:15}
# Using prefix/suffix
T1=${T#*T}
T1=${T1%Z*}
echo "$T1"
So i have an alphanumeric string 10006cc2190ab011 i am trying to add a colon after every two letters in this alphanumeric string.
this is the string : 10006cc2190ab011
i want it be - 10:00:6c:c2:19:0a:b0:11
Thanks in advance.
A sed solution:
$ echo 10006cc2190ab011 | sed 's/../&:/g; s/:$//'
10:00:6c:c2:19:0a:b0:11
Replaces each non-overlapping pair of characters with the same pair plus :. In the end removes the trailing : (if input text had even length).
str=10006cc2190ab011; str="${str//??/${.sh.match}:}"; echo ${str%:}
is doing the same replacement without the use of an external command, just using ksh-internals.
Doing the same as in sed (the other answer). Replace in $str every // two charactes ?? with / the matched string and a : (every match is kept in the ksh-variable ${.sh.match}). Then print $str without the last % ':'.
To get what is between "aa=" and either % or empty
string = "aa=value%bb"
string2 = "bb=%aa=value"
The rule must work on both strings to get the value of "aa="
I would like a BASH LANGUAGE solution if possible.
Use this:
result=$(echo "$string" | grep -o 'aa=[^%]*')
result=${result:3} # remove aa=
[^%]* matches any sequence of characters that doesn't contain %, so it will stop when it gets to % or the end of the string. $(result:3} expands to the substring starting from character 3, which removes aa= from the beginning.
I'm reading in values from an .ini file, and sometimes may get trailing or leading whitespace.
How do I amend this first line to prevent that?
db=$(sed -n 's/.*DB_USERNAME *= *\([^ ]*.*\)/\1/p' < config.ini);
echo -"$db"-
Result;
-myinivar -
I need;
-myinivar-
Use parameter expansion.
echo "=${db% }="
You don't need the .* inside the capturing group (or the semicolon at the end of line):
db="$(sed -n 's/.*DB_USERNAME *= *\([^ ]*\).*/\1/p' < config.ini)"
To elaborate:
.* matches anything at all
DB_USERNAME matches that literal string
* (a single space followed by an asterisk) matches any number of spaces
= matches that literal string
* (a single space followed by an asterisk) matches any number of spaces
\( starts the capturing group that is used for \1 later
[^ ] matches anything which is not a space character
* repeats that zero or more times
\) ends the capturing group
.* matches anything at all
Therefore, the result will be all the characters after DB_USERNAME = and any number of spaces, up to the next space or end of line, whichever comes first.
You can use echo to trim whitespace:
db='myinivar '
echo -"$(echo $db)"-
-myinivar-
Use crudini which handles these ini file edge cases transparently
db=$(crudini --get config.ini '' DB_USERNAME)
To get rid of more than one trailing space, use %% which removes the longest matching pattern from the end of the string
echo "=${db%% *}="