结合greps制作脚本来计算文件夹中的文件数

发布于 2024-11-16 01:20:05 字数 718 浏览 2 评论 0原文

我需要一些帮助来组合脚本元素以形成读取输出。

基本上,我需要获取下面列出的文件夹结构的用户的文件名,并使用文件类型 *.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

来日方长 2024-11-23 01:20:05
find /home -name '*.ano' | awk -F/ '{print $(NF-2)}' | sort | uniq -c

如果你的 awk 是正确的,这应该会给你每个用户的“*.ano”文件的数量。我经常使用 sort/uniq -c 来计算字符串实例的数量(在本例中为用户名),而不是仅计算输入行的“wc -l”。

享受。

find /home -name '*.ano' | awk -F/ '{print $(NF-2)}' | sort | uniq -c

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.

一杯敬自由 2024-11-23 01:20:05

要计算目录中 *.ano 文件的数量,您可以使用

find "$dir" -iname '*.ano' | wc -l

如果您想对某个目录中的所有目录执行此操作,则可以使用 for 循环:

for dir in * ; do
    echo "user $dir"
    find "$dir" -iname '*.ano' | wc -l
done

To count the number of *.ano files in a directory you can use

find "$dir" -iname '*.ano' | wc -l

If you want to do that for all directories in some directory, you can just use a for loop:

for dir in * ; do
    echo "user $dir"
    find "$dir" -iname '*.ano' | wc -l
done
猫性小仙女 2024-11-23 01:20:05

从文件夹中执行下面的 bash 脚本

/home/user/Drive-backup/2010 Backup/2010 Account/Jan

,它将报告每个用户的非空行数。

#!/bin/bash

#save where we start
base=$(pwd)
# get all top-level dirs, skip '.'
D=$(find . \( -type d ! -name . -prune \))

for d in $D; do
    cd $base
    cd $d
    # search for all files named *.ano and count blank lines
    sum=$(find . -type f -name *.ano -exec grep -cv '^[[:space:]]*
 {} \; | awk '{sum+=$0}END{print sum}')
    echo $d $sum
done

Execute the bash-script below from folder

/home/user/Drive-backup/2010 Backup/2010 Account/Jan

and it will report the number of non-blank lines per user.

#!/bin/bash

#save where we start
base=$(pwd)
# get all top-level dirs, skip '.'
D=$(find . \( -type d ! -name . -prune \))

for d in $D; do
    cd $base
    cd $d
    # search for all files named *.ano and count blank lines
    sum=$(find . -type f -name *.ano -exec grep -cv '^[[:space:]]*
 {} \; | awk '{sum+=$0}END{print sum}')
    echo $d $sum
done
感情废物 2024-11-23 01:20:05

这可能就是您想要的(未经测试):关联数组需要 bash 版本 4

declare -A count
cd /home/user/Drive-backup
for userdir in */*/*/*; do
    username=${userdir##*/}
    lines=$(grep -cv '^[[:space:]]
 $userdir/user.dir/*.ano | awk '{sum += $2} END {print sum}')
    (( count[$username] += lines ))
done

for user in "${!count[@]}"; do
    echo $user ${count[$user]}
done

This might be what you want (untested): requires bash version 4 for associative arrays

declare -A count
cd /home/user/Drive-backup
for userdir in */*/*/*; do
    username=${userdir##*/}
    lines=$(grep -cv '^[[:space:]]
 $userdir/user.dir/*.ano | awk '{sum += $2} END {print sum}')
    (( count[$username] += lines ))
done

for user in "${!count[@]}"; do
    echo $user ${count[$user]}
done
风吹过旳痕迹 2024-11-23 01:20:05

这是另一种方法(在 Mac OS X 10.6 上):

find -x "$PWD" -type f -iname "*.ano" -exec bash -c '
  ar=( "${@%/*}" )                 # perform a "dirname" command on every array item
  printf "%s\000" "${ar[@]%/*}"    # do a second "dirname" and add a null byte to every array item
' arg0 '{}' + | sort -uz | 
while IFS="" read -r -d '' userDir; do
  # to-do: customize output to get example output needed
  echo "$userDir"
  basename "$userDir"
  find -x "${userDir}" -type f -iname "*.ano" -print0 |
  xargs -0 -n 500 grep -hcv '^[[:space:]]*
 | awk '{ s+=$0 } END { print s }'
  #xargs -0 -n 500 grep -cv '^[[:space:]]*
 | awk -F: '{ s+=$NF } END { print s }'
  printf '%s\n' '----------'
done

Here's yet another way of doing it (on Mac OS X 10.6):

find -x "$PWD" -type f -iname "*.ano" -exec bash -c '
  ar=( "${@%/*}" )                 # perform a "dirname" command on every array item
  printf "%s\000" "${ar[@]%/*}"    # do a second "dirname" and add a null byte to every array item
' arg0 '{}' + | sort -uz | 
while IFS="" read -r -d '' userDir; do
  # to-do: customize output to get example output needed
  echo "$userDir"
  basename "$userDir"
  find -x "${userDir}" -type f -iname "*.ano" -print0 |
  xargs -0 -n 500 grep -hcv '^[[:space:]]*
 | awk '{ s+=$0 } END { print s }'
  #xargs -0 -n 500 grep -cv '^[[:space:]]*
 | awk -F: '{ s+=$NF } END { print s }'
  printf '%s\n' '----------'
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文