Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在shell脚本中将所有单词与hashtag(#)匹配_Bash_Shell_Sh - Fatal编程技术网

Bash 在shell脚本中将所有单词与hashtag(#)匹配

Bash 在shell脚本中将所有单词与hashtag(#)匹配,bash,shell,sh,Bash,Shell,Sh,有谁能给我一个快速的方法,使用shell脚本提取给定句子中的所有hashtag吗 例如: “这是一个#测试,它将#允许我#删除内容”将返回#测试#允许#删除您可能想要尝试egrep-o'.[^]+'。输出应该如下所示: #test #allow #remove 仅为提供awk的替代方案: awk '{for (i=1; i<=NF; i++) if ($i ~ /^#/) print $i}' x=$str # your original string while :; do i

有谁能给我一个快速的方法,使用shell脚本提取给定句子中的所有hashtag吗

例如:
“这是一个#测试,它将#允许我#删除内容”
将返回
#测试#允许#删除

您可能想要尝试
egrep-o'.[^]+'
。输出应该如下所示:

#test
#allow
#remove

仅为提供awk的替代方案:

awk '{for (i=1; i<=NF; i++) if ($i ~ /^#/) print $i}'
x=$str # your original string
while :; do
   if [[ $x =~ (\#[a-z]+)(.*)$ ]]; then
      echo "${BASH_REMATCH[1]}"
      x="${BASH_REMATCH[2]}"
   else
      break
   fi
done