簡體   English   中英

"<i>Assume the file name is given as command line argument.<\/i>假設文件名作為命令行參數給出。<\/b> <i>Write a shell script to count lines, characters and words<\/i>編寫一個shell腳本來計算行數、字符數和單詞數<\/b>"

[英]Assume the file name is given as command line argument. Write a shell script to count lines, characters and words

我嘗試解決問題,但我無法解決最后一部分給出的“假設文件名作為命令行參數給出”

編寫一個 shell 腳本,顯示文件中的單詞、字符、行的總數。 假設文件名作為命令行參數給出。

echo
c=$( wc -c < test.txt)
echo "Number of characters in test.txt is $c"
echo
w=$( wc -w < test.txt)
echo "Number of words in test.txt is $w"
echo
l=$( ec -l < test.txt)
echo "Number of lines in test.txt is $l"

這應該有效。 編寫腳本 the_script.sh 如下:

for F in ${*}
do
   echo
   c=$( wc -c < ${F})
   echo "Number of characters in ${F} is $c"
   echo
   w=$( wc -w < ${F} )
   echo "Number of words in ${F} is $w"
   echo
   l=$( wc -l < ${F})
   echo "Number of lines in ${F} is $l"
done

解釋一下, ${*}是傳入的命令行參數,當與上述for循環一起使用時,每個參數在循環的每次迭代中都與$F

然后在循環中,引用${F}作為文件名。

跑步:

./the_script.sh text.txt test2.txt ....

正如其他人所指出的,如果文件很大,你應該避免運行wc三次,所以這里是一種只運行一次並處理計數的方法:

WC=$( wc ${F} )
l=$( echo $WC | awk '{print $1}' )
w=$( echo $WC | awk '{print $2}' )
c=$( echo $WC | awk '{print $3}' )

這將工作

#!/bin/bash
echo "Number of characters in $1 `wc -m < $1`"
echo "Number of words in $1 `wc -w < $1`"
echo "Number of lines in $1 `wc -l < $1`"
  1. $1 :第一個參數(文件名)
  2. `` (反引號):用於執行命令
  3. 選項: -m :count 個字符 -w :count words -l :count 行
for D in ${*}

do

echo

c=$( wc -c < ${D})

echo "Number of Characters in ${D} is $c"

echo

w=$( wc -w < ${D} )

echo "Number of Words in ${D} is $w"

echo

l=$( wc -l < ${D})

echo "Number of lines in ${D} is $l"

done

**您也可以使用這種簡單的格式**

for F in test.txt
   do 
        echo
        c=$( wc -c < test.txt )
        echo "number in test.txt is $c"
    done
 echo "Number of characters in $1 is $(wc -m < $1)"
 echo "Number of words in $1 is $(wc -w < $1)"
 echo "Number of lines in $1 is $(wc -l < $1)"

您需要在打印時用 $1 替換 test.txt。 這將清除兩個測試用例..

使用這個簡單的解決方案:

wc -c ${*} | awk '{print "Number of characters in " $2 " is " $1}'
wc -w ${*} | awk '{print "Number of words in " $2 " is " $1}' 
wc -l ${*} | awk '{print "Number of lines in "$2 " is " $1}'

基本上,它在問題中給出了文件名將通過命令行參數傳遞的問題。 所以基本上你可以通過使用該命令行參數作為變量來打印文件名(它不是所有測試用例的 test.txt )。 這是我在 Tekstac 平台中被接受的代碼-

words=`cat $1 | wc -w`
char=`cat $1 | wc -c`
lines=`cat $1 | wc -l`
echo "Number of characters in $1 is $char"
echo "Number of words in $1 is $words"
echo "Number of lines in $1 is $lines"
passed=$1  
echo  
echo -n "Number of characters in $passed is "  
wc -c < $passed  
echo   
echo -n "Number of words in $passed is "  
wc -w < $passed  
echo  
echo -n "Number of letters in $passed is "  
wc -l < $passed  

試試這個它工作正常。 已清除所有測試

for F in $*

do

  echo

  c=$( wc -c < ${F} )

  echo "Number in characters in ${F} is $c"

  echo

  w=$( wc -w < ${F} )

  echo "Number in words in ${F} is $w"

  echo

  l=$( wc -l < ${F} )

   echo "Number in lines in ${F} is $l"

done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM