linux shell中如何获取单行文本字符数?例如,我想知道test.txt第四行有多少个字符,该怎么做?

如题所述

第1个回答  2012-03-26
#!/bin/bash
echo "input the linenumber"
read linenumber
count=1
cat myfile | while read line
do
if [ $linenumber -eq $count ];
then
echo "$line"
echo "$line"| wc -c
fi
count=$[ $count+1 ]
done

输出的结果应该比实际多1,因为wc命令每个换行符也算一个字符
第2个回答  2012-03-26
sed -n ‘4p’ test.txt | wc -m
注意:字符数目包含行尾的一个换行符。
第3个回答  2012-03-26
vim test.sh
sed -n '4p' test.txt|wc -c
chmod +x test.sh
./test.sh
第4个回答  2012-03-26
cat test.txt|sed -n ‘4p’ |wc -c本回答被提问者采纳
相似回答