Issue
I want to extract word1 from:
something /CLIENT_LOGIN:word1 something else
I would like to extract the first word after matching pattern /CLIENT_LOGIN:
.
Without the slash, something like this works:
A=something /CLIENT_LOGIN:word1 something else
B=$(echo $A | awk '$1 == "CLIENT_LOGIN" { print $2 }' FS=":")
With the slash though, I can't get it working (I tried putting /
and \/
in front of CLIENT_LOGIN
). I don't care getting it done with awk
, grep
, sed
, ...
Solution
Using sed
:
s='=something /CLIENT_LOGIN:word1 something else'
sed -E 's~.* /CLIENT_LOGIN:([^[:blank:]]+).*~\1~' <<< "$s"
word1
Details:
- We use
~
as regex delimiter insed
/CLIENT_LOGIN:([^[:blank:]]+)
matches/CLIENT_LOGIN:
followed by 1+ non-whitespace characters that is captured in group #1.*
on both sides matches text before and after our match\1
is used in substitution to put 1st group's captured value back in output
Answered By - anubhava Answer Checked By - Senaida (WPSolving Volunteer)