简体   繁体   English

我只想显示匹配的用户直到逗号

[英]I only want to display the matching users up until the comma

I want to print the color along wit the users who match, not all the users.我想与匹配的用户一起打印颜色,而不是所有用户。 So I want to find bob and riley and only print the data associated with them and not the other users.所以我想找到 bob 和 riley,只打印与他们相关的数据,而不是其他用户。 If there is no matches I would like the whole line just to be skipped and not display anything如果没有匹配项,我希望跳过整行而不显示任何内容

How can I achieve this?我怎样才能做到这一点?


awk '{ FS=":"; print $1 " " $4 }' /test|while read color person
do
   if [[ `echo ${users}|egrep -i "bob|riley"` ]]
   then
   printf " ${color} - ${person}\n\n"
   fi
done

used FS because in the file there are 4 field separated by the ":"使用 FS 是因为在文件中有 4 个字段由“:”分隔

Input looks something like this:输入看起来像这样:

red: :car:todd,riley,bob,greg                 
green: :jeep:todd,riley,bob,greg,thomas                                                    
black: :truck:jamie,jack,bob,travis,riley

Output currently: Output 目前:

red - todd,riley,bob,greg
green - todd,riley,bob,greg,thomas
black - jamie,jack,bob,travis,riley

Desired output所需 output

red - bob,riley
green - bob,riley
black - bob,riley

Doesn't have to be sorted like this but it would be helpful不必像这样排序,但它会有所帮助

When you have GNU grep available, you can use option-o (--only-matching)当你有 GNU grep 可用时,你可以使用选项-o (--only-matching)

echo ${person} | egrep -o -i -e "bob|riley"

will show for the first line将显示在第一行

riley
bob

Now you can combine the results with tr现在您可以将结果与tr结合起来

echo ${person} | egrep -o -i -e "bob|riley" | tr '\n' ,

gives

riley,bob,

And finally strip the trailing comma最后去掉尾随的逗号

echo ${person} | egrep -o -i -e "bob|riley" | tr '\n' , | sed -e 's/,$//'

which results in这导致

riley,bob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM