在单词字符和逗号的模式之后grep for contents

问题描述 投票:1回答:3
echo  "this is a test:foo,bar,baz']" | grep -o -E "test:.*" | awk -F: '{ print $2 }'
foo,bar,baz']

我得到']印在最后,如何只打印字符和普通,没有别的,在这种情况下,我只需要提取foo,bar,baz

linux shell grep
3个回答
0
投票

您可以使用单个awk

echo "this is a test:foo,bar,baz']" | awk -F 'test:' '{sub(/[^,[:alnum:]].*/, "", $2); print $2}'

foo,bar,baz

或者,您可以使用单个sed

echo  "this is a test:foo,bar,baz']" | sed 's/.*test://; s/[^,[:alnum:]].*//'

foo,bar,baz

0
投票
echo "this is a test:foo,bar,baz']"| awk -F: '{sub(/baz../,"baz"); print $2}'

输出

福,酒吧,闪电


0
投票

使用gnu grep perl正则表达式

$ echo  "this is a test:foo,bar,baz']" | grep -oP "(?<=test:)(\w,*)+"
foo,bar,baz
© www.soinside.com 2019 - 2024. All rights reserved.