bash 假设文件名作为命令行参数给出。写一个shell脚本来统计行数、字符数和单词数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55314154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assume the file name is given as command line argument. Write a shell script to count lines, characters and words
提问by Janvi
I tried solving the problem but I cannot solve the last part where it is given as "Assume the filename is given as command line argument"
我尝试解决这个问题,但我无法解决最后一部分给出的“假设文件名作为命令行参数给出”
Write a shell script which displays the total number of words, characters, lines in a file. Assume the file name is given as command line argument.
编写一个 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"
I had to pass 2 test cases but I'm only passing one test case as I'm unable to solve the problem assuming the filename as command line argument.
我必须通过 2 个测试用例,但我只通过了一个测试用例,因为假设文件名作为命令行参数,我无法解决问题。
回答by TenG
This should work. Write a script the_script.sh as follows:
这应该有效。编写脚本 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
To explain, ${*}
is ll the command line args passed in, and when used with teh for
loop as above each argument is assin to $F
in each iteration of the loop.
解释一下,${*}
是传入的命令行参数,当与for
上述循环一起使用时,每个参数$F
在循环的每次迭代中都是相同的。
Then within the loop, refer to ${F}
as the file name.
然后在循环内,引用${F}
为文件名。
To run:
跑步:
./the_script.sh text.txt test2.txt ....
As others have noted you should avoid running wc
three times if the files are large, so here is one way to run it just once and process the counts:
正如其他人所指出的,wc
如果文件很大,您应该避免运行3 次,因此这是一种只运行一次并处理计数的方法:
WC=$( wc ${F} )
l=$( echo $WC | awk '{print }' )
w=$( echo $WC | awk '{print }' )
c=$( echo $WC | awk '{print }' )
回答by Shivamyadav37
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
回答by Tanishka Singhal
This will work
这将工作
#!/bin/bash
echo "Number of characters in `wc -m < `"
echo "Number of words in `wc -w < `"
echo "Number of lines in `wc -l < `"
- $1: first argument (filename)
- ``(back quotes) :used to execute commands
- options: -m :count characters -w :count words -l :count lines
- $1:第一个参数(文件名)
- ``(反引号):用于执行命令
- 选项: -m :count 个字符 -w :count words -l :count 行
回答by Shubham Raikwar
**You can also go with this easy format **
**您也可以使用这种简单的格式**
for F in test.txt
do
echo
c=$( wc -c < test.txt )
echo "number in test.txt is $c"
done
回答by Robin Singh Rawat
echo "Number of characters in is $(wc -m < )"
echo "Number of words in is $(wc -w < )"
echo "Number of lines in is $(wc -l < )"
You need to replace test.txt with $1 while printing. This will clear both the test cases..
打印时需要用 $1 替换 test.txt。这将清除两个测试用例..
回答by Rakshitha S
passed=
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
回答by vinayak lakhotiya
Try this one it works fine. All tests cleared
试试这个它工作正常。清除所有测试
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