标题说明了一切。我正在尝试建立一个正则表达式,但失败了。任务是返回逗号分隔列表中的第一个字符串,该列表与“禁止”'常量字符串。被禁止的'字符串可以出现在列表中的任何位置,并且(理论上)可以在列表中多次出现。
例如(禁止'字符串=" TBD")
"TBD,Smith" --> need to return Smith
"TBD,TBD,TBD,Jones,Edwards" --> need to return Jones
"ABC,TBD,Smith" --> need to return ABC
"TBD,DEF-9gh,GHI,JKLMNOpqrst,Any old string" --> need to return DEF-9gh
那些知道该怎么做的正则表达人ninjas?
答案 0 :(得分:2)
使用grep -P
:
s="ABC,TBD,Smith"
echo "$s"|grep -oP '(^|,)\K(?!TBD)[^,]+'|head -1
ABC
s="TBD,TBD,TBD,Jones,Edwards"
echo "$s"|grep -oP '(^|,)\K(?!TBD)[^,]+'|head -1
Jones
s="TBD,DEF-9gh,GHI,JKLMNOpqrst,Any old string"
echo "$s"|ggrep -oP '(^|,)\K(?!TBD)[^,]+'|head -1
DEF-9gh
如果您的grep不支持-P
,那么这里是 awk解决方案:
echo "$s" | awk -F '(TBD,)*|,' '{print $1$2; exit}'
DEF-9gh
答案 1 :(得分:0)
我能正确理解你的问题吗?
awk
:
$ awk -F',' '{for(i=1;i<=NF;i++){if($i!="TBD"){print $i;next}}}' input.txt
Smith
Jones
ABC
DEF-9gh
符合POSIX标准的shell解决方案:
$ cat t.sh
#!/bin/sh
while read -r line; do
IFS=,
for token in ${line}; do
if [ "${token}" != TBD ]; then
echo "${token}"
continue 2
fi
done
done <<EOT
TBD,Smith
TBD,TBD,TBD,Jones,Edwards
ABC,TBD,Smith
TBD,DEF-9gh,GHI,JKLMNOpqrst,Any old string
EOT
$ ./t.sh
Smith
Jones
ABC
DEF-9gh
或只是
get_token()
(
IFS=,
for t in $@; do
[ "$t" != TBD ] && echo "$t" && break
done
)
get_token "TBD,TBD,TBD,Jones,Edwards" # => "Jones"