How to extract a variable length substring using ksh

Viewed 130

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

1 Answers

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}'
Related