正则表达式 – bash:如何检查字符串是否以’#’开头?
发布时间:2020-12-14 06:29:27 所属栏目:百科 来源:网络整理
导读:在bash中,我需要检查一个字符串是否以’#’符号开头.我怎么做? 这是我的采取 – if [[ $line =~ '#*' ]]; then echo "$line starts with #" ;fi 我想通过一个文件运行这个脚本,文件看起来像这样 – 03930#9032943929#39839 这是我的脚本 – while read line
在bash中,我需要检查一个字符串是否以’#’符号开头.我怎么做?
这是我的采取 – if [[ $line =~ '#*' ]]; then echo "$line starts with #" ; fi 我想通过一个文件运行这个脚本,文件看起来像这样 – 03930 #90329 43929 #39839 这是我的脚本 – while read line ; do if [[ $line =~ '#*' ]]; then echo "$line starts with #" ; fi done < data.in 这是我的预期产出 – #90329 starts with # #39839 starts with # 但我不能让它工作,任何想法?
没有正则表达式需要,一个模式就够了
if [[ $line = #* ]] ; then echo "$line starts with #" fi 或者,您可以使用参数扩展: if [[ ${line:0:1} = # ]] ; then echo "$line starts with #" fi (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |