String:
echo "40125512|abcd32External_SOC=ALPHA3;PCRFabcran"
I want to grab everything before the first instance of | and everything between External_SOC and ;PCRF. And store them as 2 different variables if possible.
x=40125512 y=ALPHA3
This gives me the following:
sed -e 's/|.*External_SOC=(.*);PCRF.*/1/'
40125512ALPHA3
Advertisement
Answer
EDIT: As per OP it needs to be done in a single line creation 2 of variables if this is the case then try following.
read -r x y <<<$(echo "40125512|abcd32External_SOC=ALPHA3;PCRFabcran" | sed 's/([^|]*).*=([^;]*).*/1 2/') echo "$x" 40125512 echo "$y" ALPHA3
OR use following as per anubhav sir’s comment:
read x y < <(sed -E 's~^([^|]+)|.*External_SOC=(.+);PCRF.*~1 2~' <<< "40125512|abcd32External_SOC=ALPHA3;PCRFabcran")
Could you please try following. One could use it from 2 separate commands to create 2 separate variables.
x=$(echo "40125512|abcd32External_SOC=ALPHA3;PCRFabcran" | sed 's/([^|]*).*/1/') echo "$x" 40125512 y=$(echo "40125512|abcd32External_SOC=ALPHA3;PCRFabcran" | sed 's/.*=([^;]*).*/1/') echo "$y" ALPHA3