结合greps制作脚本来计算文件夹中的文件数
我需要一些帮助来组合脚本元素以形成读取输出。
基本上,我需要获取下面列出的文件夹结构的用户的文件名,并使用文件类型 *.ano 计算该用户的文件夹中的行数
,这显示在下面的摘录中,请注意该位置从前面算起,文件名上的内容并不总是相同。
/home/user/Drive-backup/2010 备份/2010 帐户/Jan/usernameneedtogrep/user.dir/4.txt
/home/user/Drive-backup/2011 备份/2010 帐户/Jan/usernameneedtogrep/user.dir/3 .ano
/home/user/驱动器备份/2010 备份/2010 Account/Jan/usernameneedtogrep/user.dir/4.ano
awk -F/ '{print $(NF-2)}'
这将为我提供所需的用户名,但我还需要知道文件类型 *.ano 的用户文件夹中有多少非空行。我有下面的 grep 可以工作,但我不知道如何将它们放在一起,以便它可以输出一个有意义的文件。
grep -cv '^[[:space:]]*$' *.ano | awk -F: '{ s+=$2 } END { print s }'
需要示例输出
UserA 500
UserB 2
UserC 20
I need some help combining elements of scripts to form a read output.
Basically I need to get the file name of a user for the folder structure listed below and using count the number of lines in the folder for that user with the file type *.ano
This is shown in the extract below, to note that the location on the filename is not always the same counting from the front.
/home/user/Drive-backup/2010 Backup/2010 Account/Jan/usernameneedtogrep/user.dir/4.txt
/home/user/Drive-backup/2011 Backup/2010 Account/Jan/usernameneedtogrep/user.dir/3.ano
/home/user/Drive-backup/2010 Backup/2010 Account/Jan/usernameneedtogrep/user.dir/4.ano
awk -F/ '{print $(NF-2)}'
This will give me the username I need but I also need to know how many non blank lines they are in that users folder for file type *.ano. I have the grep below that works but I dont know how to put it all together so it can output a file that makes sense.
grep -cv '^[[:space:]]*
Example output needed
UserA 500
UserB 2
UserC 20
*.ano | awk -F: '{ s+=$2 } END { print s }'
Example output needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你的 awk 是正确的,这应该会给你每个用户的“*.ano”文件的数量。我经常使用 sort/uniq -c 来计算字符串实例的数量(在本例中为用户名),而不是仅计算输入行的“wc -l”。
享受。
That ought to give you the number of "*.ano" files per user given your awk is correct. I often use sort/uniq -c to count the number of instances of a string, in this case username, as opposed to 'wc -l' only counting input lines.
Enjoy.
查看wc(字数统计)。
Have a look at wc (word count).
要计算目录中 *.ano 文件的数量,您可以使用
如果您想对某个目录中的所有目录执行此操作,则可以使用 for 循环:
To count the number of *.ano files in a directory you can use
If you want to do that for all directories in some directory, you can just use a for loop:
从文件夹中执行下面的 bash 脚本
,它将报告每个用户的非空行数。
Execute the bash-script below from folder
and it will report the number of non-blank lines per user.
这可能就是您想要的(未经测试):关联数组需要 bash 版本 4
This might be what you want (untested): requires bash version 4 for associative arrays
这是另一种方法(在 Mac OS X 10.6 上):
Here's yet another way of doing it (on Mac OS X 10.6):