以下是我的代码:
#!/bin/ksh -p
NAME=$1
echo $NAME likes to drink:
grep $NAME ~/practice/database/likes
echo total number of beers $NAME likes to drink:
grep $NAME ~/practice/database/likes | wc -l
以下是我的输出:
dave likes to drink:
dave,coors
dave,bud
total number of beers dave likes to drink:
2
我如何输出“coors”和“bud”?
答案 0 :(得分:1)
您可以尝试这样的事情:
grep $NAME ~/practice/database/likes | awk -F, '{ print $2 }'
或
grep $NAME ~/practice/database/likes | cut -d , -f 2
答案 1 :(得分:0)
如果您使用sed
,那么您只需要一个程序:
sed 's/'$NAME',//' ~/practice/database/likes
或者您可以使用awk
,但不要使用grep
,因为awk
也可以使用awk -F, '/'$NAME'/{ print $2 }' ~/practice/database/likes
:
{{1}}