3
任何人都可以給我一個快速的方法來使用shell腳本提取給定句子中的所有hashtags。在shell腳本中將所有單詞與標籤(#)匹配
例如: 'This is a #test that will #allow me to #remove stuff'
將返回
任何人都可以給我一個快速的方法來使用shell腳本提取給定句子中的所有hashtags。在shell腳本中將所有單詞與標籤(#)匹配
例如: 'This is a #test that will #allow me to #remove stuff'
將返回
你可能想嘗試egrep -o '#[^ ]+'
。輸出應該是這樣的:
#test
#allow
#remove
只是提供使用awk替代:
awk '{for (i=1; i<=NF; i++) if ($i ~ /^#/) print $i}'
這裏是提取這些MATHES純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