2016-12-01 61 views
-2

编写一个shell脚本来计算文件中的行数,字符数,字数(不使用命令)。同时从出现的文件中删除单词“Linux”的出现并将结果保存到新文件中。用于计数行数的shell脚本程序

+0

http://ryanstutorials.net/bash-scripting-tutorial/ – MYGz

回答

1

这是最接近我能得到不使用任何第三方软件包...

#!/bin/bash 

count=0 
while read -r line 
do 
    count=$((count + 1)) 
done < "$filename" 
echo "Number of lines: $count" 
0
  • 萨钦巴拉德瓦给计数行的脚本。
  • 现在,要计算这些词,我们可以使用set将该行分成$#位置参数。
  • 而为了计算字符,我们可以使用参数长度:${#line}
  • 最后,要删除每个“Linux”,我们可以使用模式替换:${line//Linux}

(参看Shell Parameter Expansion

所有一起:

while read -r line 
do 
    ((++count)) 
    set -- $line 
    ((wordcount+=$#)) 
    ((charcount+=${#line}+1)) # +1 for the '\n' 
    echo "${line//Linux}" 
done < "$filename" >anewfile 
echo "Number of lines: $count" 
echo "Number of words: $wordcount" 
echo "Number of chars: $charcount"