Home > Enterprise >  How to get the 6 characters before the last occurrence of a substring in a string?
How to get the 6 characters before the last occurrence of a substring in a string?

Time:12-07

Given a string that is of the form:

variable = "info 1 keyword info 2 keyword info 3 789 info 4 keyword extra text..."

Desired output = "info 4"

I would like to write a command in bash to get info 4 given a keyword, obtaining the 6 characters before the last occurrence of the keyword. I have tried using the grep and cut tools, but I haven't been able to figure this problem out.

CodePudding user response:

With bash >= 3.0 and a regex:

[[ "$variable" =~ .*(.{6})\ keyword ]] && echo "${BASH_REMATCH[1]}"

Output:

info 4

CodePudding user response:

6 characters before the last keyword keyword, you can try this sed

$ var1="info 1 keyword info 2 keyword info 3 789 info 4 keyword extra text..."
$ sed 's/.*\(......\) keyword.*/\1/' <<< $var1
info 4

CodePudding user response:

Alternative to bash regex operator:

a=${variable%\ keyword*}
a=${a: -6}
echo "$a"

or, in POSIX shell

a=${variable%\ keyword*}
a=${a#"${a%??????}"}
echo "$a"

Both versions assume that the variable contains the keyword.

CodePudding user response:

How about a grep solution with -P option:

variable="info 1 keyword info 2 keyword info 3 789 info 4 keyword extra text..."
grep -Po '.{6}(?= keyword(?!.*keyword))' <<< "$variable"

Output:

info 4

The regex (?= keyword(?!.*keyword)) is a lookahead assertion which matches the beginning of the keyword substring which is not followed by any more keyword.

  • Related