[英]Find all the words in a unix file which begin with '#'
输入文件看起来像这样:-
mary #had a little #lamb its #fleece #was white as snow
输出应该看起来像
#had #lamb #fleece #was
使用sed
sed -r 's/(^| )[^#][^ ]+//g'
测试
echo "mary #had a little #lamb its #fleece #was white as snow" | sed -r 's/(^| )[^#][^ ]+//g'
#有#羊羔#羊毛
使用grep
grep -o "#[^ ]*"
测试
$ echo "mary #had a little #lamb its #fleece #was white as snow" | grep -o "#[^ ]*"
#had
#lamb
#fleece
#was
如果您不介意前导空格并且不想匹配中间可能包含#
单词(例如lit#tle
,也可以尝试以下操作:
grep -o -P "(\s|^)#\w+"
例:
echo "#mary #had a lit#tle #lamb its #fleece #was white as snow" | grep -o -P "(\s|^)#\w+"
#mary
#had
#lamb
#fleece
#was
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.