Can anyone give me a quick way to extract all hashtags in a given sentence using shell scripts.
For example:
'This is a #test that will #allow me to #remove stuff' would return #test #allow #remove
You may want to try egrep -o '#[^ ]+'. The output should look like:
#test
#allow
#remove
Just to provide an alternative with awk:
awk '{for (i=1; i<=NF; i++) if ($i ~ /^#/) print $i}'
And here is pure BASH way of extracting these mathes:
x=$str # your original string
while :; do
if [[ $x =~ (\#[a-z]+)(.*)$ ]]; then
echo "${BASH_REMATCH[1]}"
x="${BASH_REMATCH[2]}"
else
break
fi
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With