2

I need to extract a variable length sub string using Korn shell on Linux.

Sample String: "SID_LIST_ORADBPOC1LSN ="

Need to extract substring: "ORADBPOC1LSN"

Note: The sample substring is of variable length.

Thanks in advance.

FR

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
user13708337
  • 97
  • 1
  • 1
  • 7
  • 1
    assuming that you're looking to delete the `SID_LIST` (prefix) and the trailing `" ="`, you can use parameter substitution, i.e. `str="SID_LIST_ORADBPOC1LSN ="; varFix="${str#SID_LIST_}"; varFix=${varFix% =}; echo "$varFix"`. Output is : `ORADBPOC1LSN` . Good luck. – shellter Oct 27 '20 at 05:12
  • 1
    you should probably provide several examples of before/after values, or will all input values have the same format of `(string) + '_' + (string) + '_' + (string) + (space) + '='`? – markp-fuso Oct 27 '20 at 12:59

1 Answers1

4

With pure bash's parameter expansion capability.

var="SID_LIST_ORADBPOC1LSN ="  ##Creating shell variable here.
temp1="${var##*_}"             ##Removing everything till _ ## for longest match here.
echo "${temp1/ =/}"            ##Substituting space = with null here.
ORADBPOC1LSN

I am printing value of temp1's parameter expansion, you could save this into variable too as per your need.



OR if you want to do it in a single awk or so then try:

echo "$var" | awk -F'_| ' '{print $3}'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93