谁能给我一种使用shell脚本快速提取给定句子中所有主题标签的方法。
例如:
'This is a #test that will #allow me to #remove stuff'
将返回#test #allow #remove
你可能想试试egrep -o '#[^ ]+'
。输出应如下所示:
#test
#allow
#remove
只是为了提供 awk 的替代方案:
awk '{for (i=1; i<=NF; i++) if ($i ~ /^#/) print $i}'
这是提取这些数学的纯 BASH 方法:
x=$str # your original string
while :; do
if [[ $x =~ (\#[a-z]+)(.*)$ ]]; then
echo "${BASH_REMATCH[1]}"
x="${BASH_REMATCH[2]}"
else
break
fi
done