As the example below, I want to keep only the word before the first 'John'.
However, the pattern I applied seems to replace John from the end to the head. So I need to call sed twice.
How could I find the correct way?
PATTERN="I am John, you are John also"
OUTPUT=$( echo "$PATTERN" | sed -r "s/(^.*)([ \t]*John[ ,\t]*)(.*$)/\1/" )
echo "$OUTPUT"
OUTPUT=$( echo "$OUTPUT" | sed -r "s/(^.*)([ \t]*John[ ,\t]*)(.*$)/\1/" )
echo "$OUTPUT"
My expectation is only call sed one time. Since if "John" appears several times it will be a trouble.
By the procedure above, it will generate output as:
Firstly it matches & trims the word after the final John; then the first John.
I am John, you are
I am
I want to execute one time and get
I am