Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想计算文件中的符号数。
wc -c f1.txt | grep [0-9]
但是此代码返回 grep 找到数字的行。我只想重新运行38。如何?
您可以使用 awk:
wc -c f1.txt | awk '{print $1}'
或使用grep -o:
grep -o
wc -c f1.txt | grep -o "[0-9]\+"
或使用 bash 正则表达式功能:
re="^ *([0-9]+)" && [[ "$(wc -c f1.txt)" =~ $re ]] && echo "${BASH_REMATCH[1]}"
将数据传递到wc标准输入而不是文件:nchars=$(wc -c < f1.txt)
wc
nchars=$(wc -c < f1.txt)