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

2
  • 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. Commented Oct 27, 2020 at 5: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) + '='? Commented Oct 27, 2020 at 12:59

1 Answer 1

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}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.