从 bash 中的 wc 中获取整数
有没有办法获取 bash 中 wc 返回的整数?
基本上我想在文件名后面将行号和字数写入屏幕。
输出:文件名行数字数
这是我到目前为止所拥有的:
files=\`ls`
for f in $files;
do
if [ ! -d $f ] #only print out information about files !directories
then
# some way of getting the wc integers into shell variables and then printing them
echo "$f $lines $words"
fi
done
Is there a way to get the integer that wc returns in bash?
Basically I want to write the line numbers and word counts to the screen after the file name.
output: filename linecount wordcount
Here is what I have so far:
files=\`ls`
for f in $files;
do
if [ ! -d $f ] #only print out information about files !directories
then
# some way of getting the wc integers into shell variables and then printing them
echo "$f $lines $words"
fi
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
只是:
会完成工作。但此输出包含前缀空格,因为
wc
使数字右对齐。Just:
will do the job. But this output includes prefixed whitespace as
wc
right-aligns the number.您可以使用
cut
命令获取第一个wc
输出的单词(即行数或字数):You can use the
cut
command to get just the first word ofwc
's output (which is the line or word count):有时 wc 在不同的平台上以不同的格式输出。例如:
在 OS X 中:
在 Centos 中:
因此,仅使用 cut 可能无法检索到号码。相反,尝试
tr
删除空格字符:Sometimes wc outputs in different formats in different platforms. For example:
In OS X:
In Centos:
So using only cut may not retrieve the number. Instead try
tr
to delete space characters:根据您的布局进行必要的调整。
使用正逻辑(“是文件”)比使用负逻辑(“不是目录”)更好
Adjust as necessary for your layout.
It's also nicer to use positive logic ("is a file") over negative ("not a directory")
已接受/流行的答案不适用于 OSX。
以下任何内容都应该可以在 bsd 和 linux 上移植。
或
或
The accepted/popular answers do not work on OSX.
Any of the following should be portable on bsd and linux.
OR
OR
如果将文件名重定向到
wc
,它将在输出中忽略文件名。Bash:
或者
不要使用
for
来迭代ls
的输出,而是这样做:如果文件名包含空格,这将起作用。
如果无法使用通配符,则应该通过管道输入
while read
循环:或使用进程替换
If you redirect the filename into
wc
it omits the filename on output.Bash:
or
Instead of using
for
to iterate over the output ofls
, do this:which will work if there are filenames that include spaces.
If you can't use globbing, you should pipe into a
while read
loop:or use process substitution
如果文件很小,您可以调用两次
wc
,并使用类似以下的内容,这可以避免管道进入额外的进程:$((...))
是 bash 的算术扩展。在这种情况下,它会从wc
的输出中删除所有空格。如果您需要行数或字数,此解决方案更有意义。
If the file is small you can afford calling
wc
twice, and use something like the following, which avoids piping into an extra process:The
$((...))
is the Arithmetic Expansion of bash. It removes any whitespace from the output ofwc
in this case.This solution makes more sense if you need either the linecount or the wordcount.
使用
sed
怎么样?How about with
sed
?试试这个数字结果:
nlines=$( wc -l < $myfile )
Try this for numeric result:
nlines=$( wc -l < $myfile )
stackoverflow 上有一个很好的解决方案,其中包含示例 此处
我将在这里复制最简单的解决方案:
也许这些页面应该合并?
There is a great solution with examples on stackoverflow here
I will copy the simplest solution here:
Maybe these pages should be merged?
像这样的事情可能会有所帮助:
Something like this may help:
要 (1) 运行
wc
一次,并且 (2) 不分配任何多余的变量,请使用完整代码:
示例输出:
To (1) run
wc
once, and (2) not assign any superfluous variables, useFull code:
Example output:
已回答问题中提出的解决方案不适用于 Darwin 内核。
请考虑以下适用于所有 UNIX 系统的解决方案:
Solutions proposed in the answered question doesn't work for Darwin kernels.
Please, consider following solutions that work for all UNIX systems:
试试这个:
它创建行计数和字数(LINE 和 WC),并使用从 wc 中提取的值来增加它们(使用 $1 作为第一列的值,使用 $2 作为第二列的值),最后打印结果。
Try this:
It creates a line count, and word count (LINE and WC), and increase them with the values extracted from wc (using $1 for the first column's value and $2 for the second) and finally prints the results.
输出:
我的文件.txt
线路:10
字数:20
字节:120
Outputs :
myfile.txt
lines: 10
words: 20
bytes: 120
您必须
在您的上下文中分别使用
wc
: 的输入重定向You have to use input redirection for
wc
:respectively in your context
有史以来最简单的答案:
Most simple answer ever: