【问题标题】:script to extract string between first and second underscore of filenames in a folder and display count of such type of files [closed]用于提取文件夹中文件名的第一个和第二个下划线之间的字符串并显示此类文件的计数的脚本[关闭]
【发布时间】:2013-11-28 06:40:23
【问题描述】:

我在 linux 文件夹中有特定格式的文件名。我想提取这些文件名的第一个和第二个下划线之间的字符串,并计算该文件夹中此类文件的数量。文件名如下:

2305237803310_ABC_A05_1378414278883.hl7

20132480014907_DEF_R01_1378420192336.hl7

20132480014793_DEF_R01_1378418604889.hl7

2313642803310_ABC_A08_1378824296915.hl7

2313614403310_ABC_A08_1378823995805.hl7

2313614403310_MNY_A08_1378823995805.hl7

等等

我的脚本的输出应该给我:

ABC 3

DEF 2

MNY 1

【问题讨论】:

  • cut -d_ -f2 filename
  • 如果您提出有关代码问题的问题,我们将提供帮助,我们不会为您完成所有工作:)
  • 嗯.. 至少不是我,其他一些人已经完成了它虽然 X)
  • 将为互联网点编码:D。但说真的,它可以帮助初学者看到一些实际的解决方案。我也总是喜欢在这里看到人们处理问题的不同方式。

标签: python perl bash shell


【解决方案1】:

使用defaultdictCountersetdefault__missing__ 成语来计算它们。这里是__missing__

txt='''\
2305237803310_ABC_A05_1378414278883.hl7
20132480014907_DEF_R01_1378420192336.hl7
20132480014793_DEF_R01_1378418604889.hl7
2313642803310_ABC_A08_1378824296915.hl7
2313614403310_ABC_A08_1378823995805.hl7
2313614403310_MNY_A08_1378823995805.hl7'''

class Dicto(dict):
    def __missing__(self, key):
        self[key]=0
        return self[key]

d=Dicto()      
for line in txt.splitlines():
    k=line.split('_')       
    d[k[1]]+=1

print d  
# {'MNY': 1, 'ABC': 3, 'DEF': 2}

【讨论】:

    【解决方案2】:

    使用 dict 和 split 很容易:

    s = ["2305237803310_ABC_A05_1378414278883.hl7","20132480014907_DEF_R01_1378420192336.hl7","20132480014793_DEF_R01_1378418604889.hl7",
         "2313642803310_ABC_A08_1378824296915.hl7","2313614403310_ABC_A08_1378823995805.hl7","2313614403310_MNY_A08_1378823995805.hl7"]
    
    resultsDict = {}    
    for value in s:
    
        m = value.split("_")
    
        if len(m) > 2:    
           myString = m[1]    
           if myString in resultsDict:
               resultsDict[myString] += 1
           else:
               resultsDict.update({myString: 1})    
        else:
            print "error in the string! there are less then 2 _"
    
    print resultsDict
    

    输出:

    {'MNY': 1, 'ABC': 3, 'DEF': 2}
    

    【讨论】:

      【解决方案3】:

      我会使用正则表达式、os.listdir 和字典来跟踪计数。这样的事情相对紧凑,并且该方法可以推广到其他类似的问题。

      import re
      import os
      import collections
      
      
      def print_names():
          names_count = collections.Counter()
          regex = r'[^_]+_([^_]*)_.*'
          for file_name in os.listdir("."):
              match = re.match(regex, file_name)
              if match:
                  names_count[match.groups()[0]] += 1
      
          for name, count in names_count.items():
              print(name, count)
      
      
      if __name__ == "__main__":
          print_names()
      

      带有示例文件的输出: ABC 3 1 元 防御2

      【讨论】:

      • 因为我使用的是 python 2.6.6,所以我稍微更改了代码,它运行良好 import re import os resultsDict = {} regex = r'[^_]+_([^_]*) _.*' for file_name in os.listdir("."): m = file_name.split("_") if len(m) > 2: myString = m[1] if myString in resultsDict: resultsDict[myString] + = 1 else: resultsDict.update({myString: 1}) else: print "error in the string! there are less than 2 _" print resultsDict
      【解决方案4】:

      bash(100% 内部命令):

      #!/bin/bash
      
      declare -A ARRAY
      
      cd "/your/linux/folder"
      for TAG in *
      do TAG=${TAG#*_}; TAG=${TAG%%_*}; (( ++ARRAY[$TAG] ))
      done
      
      for TAG in ${!ARRAY[*]}
      do echo $TAG ${ARRAY[$TAG]}
      done
      

      输出:

      ABC 3
      MNY 1
      DEF 2
      

      【讨论】:

      • ...如果您希望对输出进行排序...通过管道对其进行排序 ;-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2016-05-11
      • 2018-03-17
      • 1970-01-01
      相关资源
      最近更新 更多