0

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

2 Answers2

1

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
  • The code prints G-lion-8648 when run.
  • See mkelement0's excellent answer to How do I use a regex in a shell script? for an explanation of [[ ... =~ ... ]] and BASH_REMATCH.
  • Built-in regular expression matching was added to Bash in version 3.0 (released in 2004) so the code should work on any Bash that you are likely to find on any non-prehistoric system.
pjh
  • 6,388
  • 2
  • 16
  • 17
  • Thank you so much .It is working perfectly as I requested. – Laith Hatabeh Jul 04 '23 at 19:50
  • 2
    Note that you could omit `^.*` from the beginning of the pattern, and `.*$` from the end. These make it match the entire string, but that's not necessary here since you only care about the bit in the middle. – Gordon Davisson Jul 05 '23 at 00:04
0

Using sed you could do as follows

$ echo "opp\car\G-lion-8648\bro" | sed -r 's/opp\\car\\([A-Z]+-lion-[0-9]+)\\bro/\1/'
G-lion-8648

with the same regexp for your other sample data

$ echo "opp\car\GJ-lion-2457476\bro" | sed -r 's/opp\\car\\([A-Z]+-lion-[0-9]+)\\bro/\1/'
GJ-lion-2457476
Pepe N O
  • 1,678
  • 1
  • 7
  • 11