BASH:ファイルを名前でグループ化

BASH:ファイルを名前でグループ化

百万を超えるファイルがあります。引き続き作業しなければなりません。
私のファイルのディレクトリ階層は次のとおりです。

source=/opt/output/renamed/
target=/opt/output/combine
send=/opt/output/send/combined 

まず、ソース(/opt/output/renamed)ディレクトリで1000個のファイルを繰り返し、ファイル名でグループ化する必要があります。

ファイル名==> ORACLE_gprtcp_201209221454_312312.log.gz

最初と2番目の列はそれほど重要ではありません。ただし、3番目のフィールド(タイムスタンプ)に基づいてグループ化する必要があります。

そして30分単位で分けなければなりません。たとえば、次の2つのファイルがあります。

1.ORACLE_gprtcp_201209231632_987546.log.gz 
2.ORACLE_gprtcp_201209231612_123876.log.gz 
3.ORACLE_gprtcp_201209231602_987546.log.gz 
4.ORACLE_gprtcp_201209231644_987546.log.gz  
5.ORACLE_gprtcp_201209231647_987546.log.gz 
6.ORACLE_gprtcp_201209231601_987546.log.gz 

最初の時間間隔セットは30分以内でなければなりません。

たとえば、最初のグループ化されたファイルは次のようになります。

レベル2、3、6(最初の30分)レベル1、4、5(最後の30分)

私はこのスクリプトを書こうとしています。

#!/bin/bash 
sourceFolder="/opt/rename/"
limitCount=10 # Limit for send file count for per process
renamed="/opt/combine"
target="/opt/send/combined/"

    for sf in ${sourceFolder}; do
    fileList=$(find ${sf} -type f -name "*.gz"  | sort -t '_' -k3 | head -${limitCount} ) 
    for filePath in $(echo "${fileList}"); do 
      fileName=$(basename ${filePath}) # source file name
      dirName=$(dirname ${filePath}) # source dir name
      #timeRef=$(echo ${fileName} | cut -d '_' -f 3 |  sed 's/\(.\{11\}\).*/\1/') 
      timeRef=$(echo ${fileName} | cut -d '_' -f 3 |  cut -c-11) 
      #time ref : ORACLE_gprtcp_20120923012703_3431593.log.gz

        if [ "${sf}" == "/opt/rename/" ]; then #####  combine
        #Move files to under /opt/combine/ to process files in the fastest way
        mv ${filePath} ${renamed}
        timeRef30="${group} | cut -d '_' -f 3 |  sed 's/\(.\{10\}\).*/\1/')"
        echo  $timeRef30
        for files in $(find ${renamed} -name "*${timeRef}*" | uniq)
        do
         fileGroup=$(echo $files | sort -t '_' -k 3 )
         first=$(echo ${fileGroup} | head -1 | cut -d '_' -f 4 | cut -d '.' -f 1)
         last=$(echo ${fileGroup}  | tail -1 | cut -d '_' -f 4 | cut -d '.' -f 1)           
          for group in ${fileGroup}
          do
           timeInt=$(echo ${group} | cut -d '_' -f 3 |  sed 's/\(.\{10\}\).*/\1/')
           zcatBaseName=$(dirname ${group}) #/opt/rename/
           zcatName=$(basename ${group}) 
           zcatUniq=$(echo ${group}| cut -d '_' -f 4 | cut -d '.' -f 1)
           newName=$(echo ${targetNAT}/ORACLE_gprtcp_${timeInt}000_${first}${last}.log)
           sleep 1
           echo "starting to zcat all files ${fileGroup}"
           zcat -f $(echo ${fileGroup}) >> "/opt/combine/ORACLE_gprtcp_${timeInt}000_${first}${last}.log"
           gzip "/opt/infolog/output/iotest/24/combine/ORACLE_gprtcp_${timeInt}000_${first}${last}.log"
           rm -f $(echo ${fileGroup})
           sleep 4                              
          done
         done
         fi 
done 
done

30分以内にファイルを正常にグループ化し、zcatして新しいファイルにする方法について提案を与えることができる人はいますか?

事前にありがとう

ベストアンサー1

残念ながら、完全な回答をする時間がなく、役に立ついくつかのヒントだけを教えてください。

私はちょうど関連ファイルを印刷し、Unixの時間に基づいてソートしました(一般/人が読むことができる時間よりも良いことがわかりました)。

find $PWD -type f -printf '%T@ %p\n' | sort -nb

次に、30分の開始時間の参照点として、30分のグループの最初のメンバーのUnix時間を保存し、現在のファイルのUnixタイムスタンプ(1800より大きい場合)との差を計算して、新しいグループを作成します。追加できます。現在のグループに。次のように:

#!/bin/bash
#1800 s = 30 min
#unix time 86400s = 1 day

fileList=$(find $PWD -type f -printf '%T@ %p\n' | sort -nb)
## for debugging:
# fileList=$(find $PWD -type f -printf '%T@ %t %p\n' | sort -nb)

org_IFS=$IFS
IFS=$'\n'
group_start_time=0
for line in $fileList; do
    current_time=$(echo $line | awk '{print $1}')
    if [ $group_start_time -eq 0 ] ; then
        group_start_time=$current_time
    else
        delta=$(($current_time - $group_start_time))
        #echo $delta
        if [ $delta -lt 1801 ] ; then
            echo $line
        else
            echo -e "\nnew group:\n$line"
            group_start_time=$current_time
        fi
    fi
done
IFS=$org_IFS

そこからファイルパスを目的のファイルにリダイレクトできます(>>使用)。次に、mvそのファイルのリストをそのディレクトリで実行します。

これが役に立つことを願っています。 :)

編集:log.gzファイルグループをターゲットディレクトリのソース(あなたの)ファイルに書き込むようにスクリプトを変更しました/opt/rename/(あなたのものとします)。/opt/send/combined/修正されたコードは次のとおりです。

#!/bin/bash
#1800 s = 30 min
#unix time 86400s = 1 day

sourceFolder="/opt/rename/"
target="/opt/send/combined/"

path_to_file=$target
current_file="ORACLE_gprtcp_000.log.gz"

fileList=$(find $sourceFolder -type f -name '*.log.gz' -printf '%T@ %p\n' | sort -nb)
## for debugging:
# fileList=$(find $PWD -type f -printf '%T@ %t %p\n' | sort -nb)

echo ${fileList[0]}

org_IFS=$IFS
IFS=$'\n'
group_start_time=0

for line in $fileList; do
    current_time=$(echo $line | awk '{print $1}')
    if [ $group_start_time -eq 0 ] ; then
        group_start_time=$current_time
        hr_time=$( date -d @$current_time +%F_%0k%0M )
        current_file="ORACLE_gprtcp_"$hr_time".log.gz"
    else
        delta=$(($current_time - $group_start_time))
        #echo $delta
        if [ $delta -lt 1801 ] ; then
            # just append file path to current_file
            echo $line | awk '{print $2}' >> $path_to_file"/"$current_file
            echo $line
        else
            # construct new filename based on time of the first member of the group
            hr_time=$( date -d @$current_time +%F_%0k%0M )
            current_file="ORACLE_gprtcp_"$hr_time".log.gz"

            # create file, append file path to current_file
            echo $line | awk '{print $2}' >> $path_to_file"/"$current_file
            echo -e "\nnew group:\n$line"

            group_start_time=$current_time
        fi
    fi
done

IFS=$org_IFS

おすすめ記事