How to remove the last occurance of back slash in string in ksh shell

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo '$var'
{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=
</code>
<code>var=$(sed -n 's/username=(.*)/1/p' security/boot.properties); echo '$var' {AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q= </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
</code>
<code>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 </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var=$(sed '/username=/!d; s///; s/\=$/=/' security/boot.properties)
</code>
<code>var=$(sed '/username=/!d; s///; s/\=$/=/' security/boot.properties) </code>
var=$(sed '/username=/!d; s///; s/\=$/=/' security/boot.properties)

To remove a final backslash, wherever it is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var=$(sed '/username=/!d; s///; s/(.*)\/1/' security/boot.properties)
</code>
<code>var=$(sed '/username=/!d; s///; s/(.*)\/1/' security/boot.properties) </code>
var=$(sed '/username=/!d; s///; s/(.*)\/1/' security/boot.properties)

To remove all backslashes:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var=$(sed '/username=/!d; s///; s/\//g' security/boot.properties)
</code>
<code>var=$(sed '/username=/!d; s///; s/\//g' security/boot.properties) </code>
var=$(sed '/username=/!d; s///; s/\//g' security/boot.properties)

0

Assumptions/understandings:

  • OP has stated the entry in security/boot.properties may include multiple characters
  • 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ cat bp
ignore=this-line
username={AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=
ignore=this-line
</code>
<code>$ cat bp ignore=this-line username={AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q= ignore=this-line </code>
$ 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ var=$(sed -n 's/username=(.*)/1/p' bp)
$ var="${var:0:${#var}-2}${var: -1}"
$ typeset -p var
var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q='
</code>
<code>$ var=$(sed -n 's/username=(.*)/1/p' bp) $ var="${var:0:${#var}-2}${var: -1}" $ typeset -p var var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=' </code>
$ 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 variable var
  • keep in mind first character is in position 0 and if there are n characters then the last position is n-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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ var=$(sed -n 's/\([^]*)$/1/; s/username=(.*)/1/p' bp)
$ typeset -p var
var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q='
</code>
<code>$ var=$(sed -n 's/\([^]*)$/1/; s/username=(.*)/1/p' bp) $ typeset -p var var='{AES}BjW32/nnL0m78BJ/ZOEoVzXmEWzVBVZx+8Q=' </code>
$ 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 literal character 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ ksh --version
version sh (AT&T Research) 93u+m/1.0.0-beta.2 2021-12-17
</code>
<code>$ ksh --version version sh (AT&T Research) 93u+m/1.0.0-beta.2 2021-12-17 </code>
$ 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: \:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var=$( perl -ne 's{(.*)\}{$1} and print;' security/boot.properties )
</code>
<code>var=$( perl -ne 's{(.*)\}{$1} and print;' security/boot.properties ) </code>
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 switches
  • perldoc perlre: Perl regular expressions (regexes)

This MAY be what you need:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sed -n 's/username=(.*)[]([^]*)/12/p' security/boot.properties
</code>
<code>sed -n 's/username=(.*)[]([^]*)/12/p' security/boot.properties </code>
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):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>awk 'match($0,/username=.*\/) { print substr($0,RSTART+9,RLENGTH-8) substr($0,RSTART+RLENGTH+1) }' file
</code>
<code>awk 'match($0,/username=.*\/) { print substr($0,RSTART+9,RLENGTH-8) substr($0,RSTART+RLENGTH+1) }' file </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật