Reputation: 35
If my variable has data as,
test="score=5,grade=d,pass=f,"
Anyway I can extract the data/rewrite the data from/into the variable as,
test="score,grade,pass"
I do not need the data between =
, &
, and ,
characters.
Upvotes: 0
Views: 189
Reputation: 46886
In pure bash (no external tools like sed or awk or perl required), you can use "Parameter Expansion" to manipulate strings. You can read about this in Bash's man page.
#!/usr/bin/env bash
test="score=5,grade=d,pass=f,"
# Use the "extglob" shell option for "extended" notation on patterns.
shopt -s extglob
# First, remove the bits from equals to before each comma...
test="${test//=+([^,]),/,}"
# Next, remove the trailing comma.
test="${test%,}"
echo "$test"
And here's a demo.
Upvotes: 5
Reputation: 174844
You may use sed.
echo "score=5,grade=d,pass=f," | sed 's/=[^,]*\(,$\|\)//g'
or
echo "score=5,grade=d,pass=f," | sed 's/=[^,]*\|,$//g'
Upvotes: 0