bashunixawksedaix

How to get a matched pattern including the previous letters and front side letters until it reaches a specific letter in BASH (AIX)


For example:

You have this string: opp\car\G-lion-8648\bro

I want G-lion-8648 part from this string but G and 8648 are changeable from valuable side or counting side what I mean is that G might be GJ or J and 8648 might be a different number it could be 008324 or 2457476 but the only constant thing is that -lion- will remain as it is.

I want a command that could get me this pattern ->G-lion-8648 which will able for me to get all the previous letters of this -lion- until it reaches \ character and get all the frontside numbers or characters until it reaches \ character to get the exact pattern.

Please consider that I am working on BASH using AIX operating system and the solution would be preferable if sed or awd are used in the solution because of the limitations in AIX.

How to exclude -sources pattern in bash with space in file name Use grep --exclude/--include syntax to not grep through certain files I used some of these links but with no clear answer.

Regards


Solution

  • This pure Bash code shows how to extract the wanted pattern from a variable (str) and assign it to another variable (lion_str):

    str='opp\car\G-lion-8648\bro'
    
    lion_rx='^.*[\\]([^\\]+-lion-[0-9]+)\\.*$'
    
    if [[ $str =~ $lion_rx ]]; then
        lion_str=${BASH_REMATCH[1]}
        printf '%s\n' "$lion_str"
    fi