我有一个脚本,它获取本地日志文件,读取每一行、卡的 ID 和 IO 类型,提取相关数据字符串,将该字符串与定义文件进行比较,然后为每个位分配 T 或 F 值。然后,它创建一个新的日志文件,其中包含人类可读的位名称和 T/F 值。
以下是我输入的一些示例行:
[09:00:15] STA8 09:58:47 28DEC23 I/O In 07 0000 0000 0000 0000
[09:00:15] STA8 09:58:47 28DEC23 I/O In 08 0000 0010 0000 0000
[09:00:15] STA8 09:58:47 28DEC23 I/O Out 07 --00 ++++ ++++ ++00
[09:00:15] STA8 09:58:47 28DEC23 I/O Out 08 ++++ ++0+ ++++ 0000
以下是一些输出示例行:
[09:00:15] STA8 09:58:47 28DEC23 I/O In 07 1:F 2:F 3:F ...
[09:00:15] STA8 09:58:47 28DEC23 I/O In 08 1:F 2:F 3:F ...
[09:00:15] STA8 09:58:47 28DEC23 I/O Out 07 1:F 2:F 3:F 4:F 5:T 6:T 7:T 8:T 9:T 10:T 11:T 12:T ...
[09:00:15] STA8 09:58:47 28DEC23 I/O Out 08 1:T 2:T 3:T 4:T 5:T 6:T 7:F 8:T ...
该脚本运行良好,但对于不是很长的输入文件,可能需要大约 18 秒才能完成。运行 sed 的同一个输入文件,其中包含一长串要查找和制作的更改,几乎是即时的。我不知道为什么这个要花这么长时间。这个确实引用了我制作的包含每一位名称的txt文件,所以我想知道它是否每次都会进行太多不必要的读取,也许我可以只加载它们一次?
这是我的脚本:
#!/bin/bash
#Variables
d=$(date +%Y-%m-%d_%H. -d '1 hour ago')
# Function to read label files and store labels in arrays
read_label_files() {
local station_id="$1"
local io_type="$2"
local card_number="$3"
local label_file="s${station_id:3}${io_type}${card_number}.txt"
local label_array=()
if [ -f "$label_file" ]; then
IFS=',' read -ra label_array < "$label_file"
fi
echo "${label_array[@]}"
}
# Function to add extracted string into an array
data_string_array=()
for ((i=0; i<${#data_string}; i++)); do
data_string_array[i]="${data_string:i:1}"
done
# Function to convert input values to T or F
convert_input_values() {
local data_string="$1"
local output_string=""
for (( i=0; i<${#data_string}; i++ )); do
if [ "${data_string:$i:1}" == "1" ]; then
output_string+="T "
else
output_string+="F "
fi
done
echo "$output_string"
}
# Function to convert output values to T or F
convert_output_values() {
local data_string="$1"
local output_string=""
for (( i=0; i<${#data_string}; i++ )); do
if [ "${data_string:$i:1}" == "+" ]; then
output_string+="T "
else
output_string+="F "
fi
done
echo "$output_string"
}
# Initialize arrays for inputs and outputs
declare -A input_changes
declare -A output_changes
# Process the input file
while read -r line; do
timestamp=$(echo "$line" | awk '{print $1}')
station_id=$(echo "$line" | awk '{print $2}')
io_type=$(echo "$line" | awk '{print $6}')
card_number=$(echo "$line" | awk '{print $7}')
logdate=$(echo "$line" | awk '{print $4}')
# Remove spaces from the data string
data_string=$(echo "$line" | awk '{$1=$2=$3=$4=$5=$6=$7=""; print $0}' | tr -d ' ')
if [ "$io_type" = "In" ]; then
# Read input label file and store labels in an array
input_labels=($(read_label_files "$station_id" "i" "$card_number"))
# Convert input values to T or F
converted_values=($(convert_input_values "$data_string"))
# Store current values for comparison
current_values="${input_changes[$station_id,$card_number]}"
input_changes[$station_id,$card_number]="$data_string"
# Print the output line with labels and T/F values
output_line="$timestamp $station_id $logdate I/O In $card_number -"
if [ -z "$current_values" ]; then
# echo "NEW"
for ((i=0; i<${#input_labels[@]}; i++)); do
output_line+=" ${input_labels[i]}:${converted_values[i]}"
done
else
# echo "UPDATE"
for ((i=0; i<${#current_values}; i++)); do
current_array[i]="${current_values:i:1}"
done
#current_array=($current_values)
for ((i=0; i<${#data_string}; i++)); do
data_string_array[i]="${data_string:i:1}"
done
#echo "old ${current_array[@]}"
#echo "new ${data_string_array[@]}"
for ((i=0; i<${#input_labels[@]}; i++)); do
#echo "Comparing index $i: data_string_array[${i}] = ${data_string_array[i]}, current_array[${i}] = ${current_array[i]}"
if [ "${data_string_array[i]}" != "${current_array[i]}" ]; then
output_line+=" ${input_labels[i]}:${converted_values[i]}"
fi
done
#echo "Output Line: $output_line"
fi
echo "$output_line" >> /home/logs/IO.$d.txt
elif [ "$io_type" = "Out" ]; then
# Read output label file and store labels in an array
output_labels=($(read_label_files "$station_id" "o" "$card_number"))
# Convert output values to T or F
converted_values=($(convert_output_values "$data_string"))
# Store current values for comparison
current_values="${output_changes[$station_id,$card_number]}"
output_changes[$station_id,$card_number]="$data_string"
# Print the output line with labels and T/F values
output_line="$timestamp $station_id $logdate I/O Out $card_number -"
if [ -z "$current_values" ]; then
# echo NEW
for ((i=0; i<${#output_labels[@]}; i++)); do
output_line+=" ${output_labels[i]}:${converted_values[i]}"
done
else
# echo UPDATE
for ((i=0; i<${#current_values}; i++)); do
current_array[i]="${current_values:i:1}"
done
#current_array=($current_values)
for ((i=0; i<${#data_string}; i++)); do
data_string_array[i]="${data_string:i:1}"
done
#echo "old ${current_array[@]}"
#echo "new ${data_string_array[@]}"
for ((i=0; i<${#output_labels[@]}; i++)); do
#echo "Comparing index $i: data_string_array[${i}] = ${data_string_array[i]}, current_array[${i}] = ${current_array[i]}"
if [ "${data_string_array[i]}" != "${current_array[i]}" ]; then
output_line+=" ${output_labels[i]}:${converted_values[i]}"
fi
done
fi
echo "$output_line" >> /home/logs/IO.$d.txt
fi
done < /home/logs/DataLog.$d.txt
答案1
答案是不要使用 bash 来做这种事情。我将为此学习Python。