[英]How to divide the number of words from the number of characters of a file in bash script using arithmetic expansion and wc
[英]How to format output for number of lines, words, characters for a file in bash?
我正在嘗試編寫一個shell腳本,該腳本將計算Bash中的行數,單詞數和字符數。
echo "Enter file name"
read file
if [ -f $file ]
then
echo "The number of lines in $file are "
echo $(wc -l $file | cut -d " " -f1 )
fi
程序確實獲取輸出,但是剪切部分未格式化輸出。 我檢查了cut和wc的語法。 如何獲得結尾處沒有文件名的行數,這是wc命令的默認特征?
這是我現在得到的輸出。
Enter file name
pow.sh
The number of lines in pow.sh are
這是必需的。
Enter file name
pow.sh
The number of lines in pow.sh are 3.
省略文件名的典型方法是首先避免將其提供給wc:
wc -l < $file
因此,您最終得到:
printf "The number of lines in $file is "
wc -l < $file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.