Issue
How to get a value after remember=
using shell script from the following case:
case 1:
String= " password sufficient shape sha512 remember=5"
case 2:
String= " password sufficient shape remember=5 sha512"
How I use sed command to get a value after =
from the both cases. the output should be 5
Solution
With sed
: just match whole the string with the remember=
followed by the backreference. Inside the backreference match the characters you want to match the value. And substitute the whole string for the backreference.
String=" password sufficient shape remember=5 sha512"
sed 's/.*remember=\([0-9]\{1,\}\).*/\1/' <<<"$String"
would output:
5
With bash
: Remove everything up until remember=
and then remove everything from the back up until a space:
String=" password sufficient shape remember=5 sha512"
a=${String##*remember=};
a=${a%% *};
echo $a
In awk
: iterate over the arguments and check if it's an "remember". Then extract the data after the =
:
String=" password sufficient shape remember=5 def";
<<<"$String" awk '{ for (i=0;i<=NF;++i) { b=$i; c=$i; gsub(/=.*/, "", b); if (b == "remember") { gsub(/.*=/, "", c); print c; } } }'
Note: the both similar source lines from your question
String= " password sufficient shape sha512 remember=5"
Both give me similar
bash: password sufficient shape sha512 remember=5: command not found
error.
Answered By - KamilCuk