var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo '$var'
{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=
Expected output is
{AES}BjW32/nnL0m78ef5G5B5tBJ/ZOEoVzXmEWzVBVZx+8Q=
As you may see the last should not display in the output.
I’m on solaris + ksh shell.
Can you please suggest.
I tried the below in vain:
var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo "${var%/}"
var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo "${var%/}" | sed 's/\$//'
var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo "${var%/}" | sed 's/\=$//'
var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo "${var%/}" | awk '{gsub(/\$/,"")}1'
var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo "${var%/}" | rev | cut -c 2- | rev
None of them worked.
8
Since you are using sed
already, you could just add a second substitution instead of piping into an extra program.
If the backslash appears before an equal sign, as the final two characters on the line:
var=$(sed '/username=/!d; s///; s/\=$/=/' security/boot.properties)
To remove a final backslash, wherever it is:
var=$(sed '/username=/!d; s///; s/(.*)\/1/' security/boot.properties)
To remove all backslashes:
var=$(sed '/username=/!d; s///; s/\//g' security/boot.properties)
0
Assumptions/understandings:
- OP has stated the entry in
security/boot.properties
may include multiplecharacters
- we want to remove only the last
character
- the last
character is always in the next-to-last position (per OP’s failed coding attempt with
rev | cut -c 2-
)
For demonstration purposes:
$ cat bp
ignore=this-line
username={AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=
ignore=this-line
Focusing on the last character residing in the next-to-last position (and assuming we don’t know the last character) we can make use of
ksh's
builtin substring capabilities:
$ var=$(sed -n 's/username=(.*)/1/p' bp)
$ var="${var:0:${#var}-2}${var: -1}"
$ typeset -p var
var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q='
Where:
${#var}
– length of string stored in variablevar
- keep in mind first character is in position
0
and if there aren
characters then the last position isn-1
${var:0:${#var}-2}
– substring from position 0 to 2nd from last (ie, ignore last 2 characters)${var: -1}
– substring starting from last character to end of string (ie, last character)
If we’re not sure where the last character is located in the string we can prepend OP’s current
sed
script with an additional replacement:
$ var=$(sed -n 's/\([^]*)$/1/; s/username=(.*)/1/p' bp)
$ typeset -p var
var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q='
Where the 1st replacement reads::
\
– a literalcharacter followed by …
([^]*)$
– capture group consisting of 0 or more non-characters up to the end of the string
$
Both of these solutions tested on an Ubuntu 22.04 host running ksh 93
:
$ ksh --version
version sh (AT&T Research) 93u+m/1.0.0-beta.2 2021-12-17
1
Use this Perl one-liner. Note that the backslash needs to be escaped like so:
\
:
var=$( perl -ne 's{(.*)\}{$1} and print;' security/boot.properties )
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-n
: Loop over the input one line at a time, assigning it to $_
by default.
s{PATTERN}{REPLACEMENT}
: Replace regex PATTERN with REPLACEMENT.
(.*)\
: 0 or more characters (captured into the variable $1
), followed by a literal backslash ().
See also:
perldoc perlrun
: how to execute the Perl interpreter: command line switchesperldoc perlre
: Perl regular expressions (regexes)
This MAY be what you need:
sed -n 's/username=(.*)[]([^]*)/12/p' security/boot.properties
or maybe you’d be better off with awk (nawk
or /usr/xpg[46]/bin/awk
on Solaris, not the old, broken default /usr/bin/awk
):
awk 'match($0,/username=.*\/) { print substr($0,RSTART+9,RLENGTH-8) substr($0,RSTART+RLENGTH+1) }' file
but without knowing what the input looks like that’s obviously just an untested guess and even if the above work for you there may be (actually, probably are) better ways of getting the output you want.