【问题标题】:Combining greps to make script to count files in folder结合greps制作脚本来计算文件夹中的文件
【发布时间】:2011-06-17 09:44:40
【问题描述】:

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

基本上我需要为下面列出的文件夹结构获取用户的文件名,并使用文件类型为 *.ano 的用户计算文件夹中的行数

这显示在下面的摘录中,请注意文件名上的位置从前面开始计算并不总是相同的。

/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)}'

这将为我提供所需的用户名,但我还需要知道文件类型 *.ano 的用户文件夹中有多少非空行。我有下面的 grep,但我不知道如何将它们放在一起,以便它可以输出一个有意义的文件。

grep -cv '^[[:space:]]*$' *.ano | awk -F: '{ s+=$2 } END { print s }'

需要示例输出

UserA   500
UserB 2
UserC 20

【问题讨论】:

  • 其中一些答案即使完美无瑕也令人恐惧。 ar=( "${@%/*}" ) printf "%s\000" "${ar[@]%/*}" 看起来完全是胡言乱语,这是作为一个已经编写 Perl 十多年的人说的。如果这是您将要使用一段时间的东西,请帮自己一个忙,并在选择解决方案时考虑可维护性。

标签: linux bash scripting awk grep


【解决方案1】:
find /home -name '*.ano' | awk -F/ '{print $(NF-2)}' | sort | uniq -c

如果你的 awk 是正确的,那应该给你每个用户的 "*.ano" 文件的数量。我经常使用 sort/uniq -c 来计算字符串实例的数量,在这种情况下是用户名,而不是 'wc -l' 只计算输入行。

享受吧。

【讨论】:

    【解决方案2】:

    看看wc (word count)

    【讨论】:

    • @overbose 谢谢,不知道 wc 但仍然需要组合所有元素以获得输出的文本文件
    【解决方案3】:

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

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

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

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

    【讨论】:

      【解决方案4】:

      从文件夹执行下面的 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
      

      【讨论】:

        【解决方案5】:

        这可能是您想要的(未经测试):关联数组需要 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
        

        【讨论】:

          【解决方案6】:

          这是另一种方法(在 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
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-08-03
            • 1970-01-01
            • 2017-11-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多