根据日期值计算linux目录中出现的次数

问题描述 投票:0回答:2

所以我有很多文件想要根据日期检查文件计数,但问题是文件中的日期有时会重叠。例如,文件可能如下所示

发行日期 cert_id 连续剧
2023-05-14 06:00:00 123 abca234
2023-05-14 23:59:00 456 qascmal
2023-05-15 00:00:00 789 acmaal
2023-05-16 12:10:00 1231 acmkla

在目录中,我有一堆文件,名称中可能有两个或更多日期,因此无法知道每个日期有多少个证书。例如 2023-05-15-2023-05-18.csv.gmz、2023-05-18-2023-05-19-2023.csv.gmz 等 有没有一种快速简便的方法可以根据上面的发布日期来计算这些文件,显然问题日期不是唯一的,但序列号/cert_id 是唯一的。所以我可以结合使用它。 我想做的是根据给定日期以编程方式检查计数,但我不确定查询这些文件是否太高效,也许最好在这些文件的来源上使用 API。但我想我会问。

因此,如果我输入 2023-05-14 作为给定日期,我将得到两个条目,而 15 日我将得到 1 个条目,第 16 日也会得到 1 个条目。

重要更新:我忘记有时一个文件末尾的证书会延续到下一个文件的开头,所以我必须考虑重复项。现在我有这个

zgrep -c -F '2023-05-11' *2023-05-11*.gz | awk -F: '{n+=$2} END {print n}'
但没有考虑到这一事实。

linux csv count wc
2个回答
2
投票

连接所有未压缩的文件,然后通过管道将其传输到

grep
:

zcat *2023-05-11*.gz | grep -c -F 2023-05-11

0
投票

回到这个问题来解决重叠问题,我创建了一个可以解决这个问题的 bash 脚本。


# Create an array to store the distinct IDs
declare -A distinct_ids

# Record the start time
start_time=$(date +%s)

# Loop through all the compressed archive files in the current directory
for file in ./*.gz; do
    # Extract the appropriate column (serial number) from the CSV file and append to a temporary file
    zcat "$file" | awk -F',' '{print $4}' >> extracted_ids.txt
done

# Sort and count the unique IDs using sort and uniq
sort extracted_ids.txt | uniq > unique_ids.txt

# Count the number of distinct unique IDs
num_unique_ids=$(wc -l < unique_ids.txt)

# Record the end time
end_time=$(date +%s)

# Calculate the time taken
duration=$((end_time - start_time))

# Display the result
echo "Number of distinct unique IDs: $num_unique_ids"
echo "Time taken: $duration seconds"

# Clean up temporary files
rm extracted_ids.txt unique_ids.txt
© www.soinside.com 2019 - 2024. All rights reserved.