Reputation: 457
How to remove words that do not start with a specific character by sed
?
Sample:
echo "--foo imhere -abc anotherone" | sed ...
Result must be;
"--foo -abc"
Upvotes: 1
Views: 1336
Reputation: 195079
gnu sed with -r
:
kent$ echo "--foo imhere -abc anotherone" | sed -r 's/^|\s[^-]\S*//g'
--foo -abc
However I prefer awk to solve it, more straightforward:
awk '{for(i=1;i<=NF;i++)$i=($i~/^-/?$i:"")}7'
output:
--foo -abc
Upvotes: 1
Reputation: 13725
echo "--foo imhere -abc anotherone" |\
sed -e 's/^/ /g' -e 's/ [^-][^ ]*//g' -e 's/^ *//g'
The first and last -e commands are needed if only when the first word can be wrong either.
Upvotes: 2
Reputation: 30995
You can use ssed
to enable PCRE regex and then you can use this one:
(?<!-)\b\w+
echo "--foo imhere -abc anotherone" | ssed 's/(?<!-)\b\w+//'
Upvotes: 0