【发布时间】:2016-04-18 10:41:38
【问题描述】:
我正在验证管道分隔文件中的几列。我的第二列默认使用“*”。
例如要验证的文件数据:
abc|* |123
def|** |456
ghi|* |789
由于数据错误,第二条记录有 2 颗星。
我把它写成:
Value_to_match="*"
unmatch_count=cat <filename>| cut -d'|' -f2 | awk '{$1=$1};1' | grep -vw "$Value_to_match" | sort -n | uniq | wc -l
echo "unmatch_count"
这让我算作 0,而我期待 1(对于 **),因为我使用了 -w 与 grep 完全匹配和 -v 反转匹配。
我如何 grep **?
【问题讨论】:
-
另外,请注意
unmatch_count=cat <file> | ...不起作用。如果要将命令的输出存储在变量中,例如var=$(command)。 -
管道
cat到cut和cut到awk和awk到grep简直太疯狂了。awk -F '\t' -v x="$Value_to_match" '$2 != x { a[$2]++ } END { for (s in a) printf("%8d%s\n", a[s], s }' filename(猜测一下你真正想要什么)。 -
大学里没有人教shell脚本了吗?好悲伤...