次のデータを含む2つのファイルがあります。
ファイル1
22:50:48] Return_M
22:50:49] Return_A
22:50:50] Return_F
22:50:51] Return_K
22:50:52] Return_Y
ファイル2
22:50:48] <0> X led_required
22:50:48] <0> X start_rules
22:50:48] <0> X leadstart
22:50:49] <0> X asynchronous_start
22:50:49] <0> X controldown
22:50:49] <0> X select_set(3)
22:50:49] <0> X limiting_rules
22:50:50] <0> X stock_manager
22:50:50] <0> X led_blink
22:50:50] <0> X start_required
22:50:51] <0> X control_down
22:50:51] <0> X select_set(3)
22:50:51] <0> X start_rules
22:50:52] <0> X stock_manager
22:50:52] <0> X blink_led
以下を出力するファイルが必要です。
22:50:48] Return_M <0> X led_required
22:50:48] Return_M <0> X start_rules
22:50:48] Return_M <0> X leadstart
22:50:49] Return_A <0> X asynchronous_start
22:50:49] Return_A <0> X controldown
22:50:49] Return_A <0> X select_set(3)
22:50:49] Return_A <0> X limiting_rules
22:50:50] Return_F <0> X stock_manager
22:50:50] Return_F <0> X led_blink
22:50:50] Return_F <0> X start_required
22:50:51] Return_K <0> X control_down
22:50:51] Return_K <0> X select_set(3)
22:50:51] Return_K <0> X start_rules
22:50:52] Return_Y <0> X stock_manager
22:50:52] Return_Y <0> X blink_led
デフォルトでは、ファイル1の戻りコードをファイル2に入れますが、一致する時間にのみ入れます。
ベストアンサー1
目的のタスクを実行する2つの(わずかに異なる)スクリプト、つまりPythonスクリプトとBashスクリプトが含まれていました。
Pythonソリューション
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""prefix_join.py"""
import sys
input_1 = sys.argv[1]
input_2 = sys.argv[2]
# Initialize a list to store prefixes as they occur
prefix_list = list()
# Parse the first input file
data_1 = dict()
with open(input_1, "r") as file_1:
for line in file_1:
# Remove trailing and leading whitespace
line = line.strip()
# Split the string on the first whitespace character
prefix, sep, suffix = line.partition(" ")
# Add the prefix the list of prefixes
if prefix not in prefix_list:
prefix_list.append(prefix)
# Add the prefix to the first data dictionary
if prefix not in data_1:
data_1[prefix] = list()
# Add the suffix to the data dictionary
data_1[prefix].append(suffix)
# Parse the second input file
data_2 = dict()
with open(input_2, "r") as file_2:
for line in file_2:
# Remove trailing and leading whitespace
line = line.strip()
# Split the string on the first whitespace character
prefix, sep, suffix = line.partition(" ")
# Add the prefix the list of prefixes
if prefix not in prefix_list:
prefix_list.append(prefix)
# Add the prefix to the first data dictionary
if prefix not in data_2:
data_2[prefix] = list()
# Add the suffix to the data dictionary
data_2[prefix].append(suffix)
# Output the joined data
for prefix in prefix_list:
for value_1 in data_1.get(prefix, list()):
for value_2 in data_2.get(prefix, list()):
output_line = "{} {} X {}".format(prefix, value_1, value_2)
print(output_line)
次のように実行できます。
python prefix_join.py file-1.txt file-2.txt
サンプルデータでは、次の出力が生成されます。
22:50:48] Return_M X <0> X led_required
22:50:48] Return_M X <0> X start_rules
22:50:48] Return_M X <0> X leadstart
22:50:49] Return_A X <0> X asynchronous_start
22:50:49] Return_A X <0> X controldown
22:50:49] Return_A X <0> X select_set(3)
22:50:49] Return_A X <0> X limiting_rules
22:50:50] Return_F X <0> X stock_manager
22:50:50] Return_F X <0> X led_blink
22:50:50] Return_F X <0> X start_required
22:50:51] Return_K X <0> X control_down
22:50:51] Return_K X <0> X select_set(3)
22:50:51] Return_K X <0> X start_rules
22:50:52] Return_Y X <0> X stock_manager
22:50:52] Return_Y X <0> X blink_led
カンクンソリューション
#!/usr/bin/env bash
# prefix-join.sh
# Get the input files as command-line arguments
input_file_1="$1"
input_file_2="$2"
# Set the internal field separator to be a newline (don't include spaces or tabs)
IFS=$'\n'
# Iterate over the lines of the first input file
for line_1 in $(cat "${input_file_1}"); do
# Split the line on the first space
prefix_1="${line_1/ */}";
suffix_1="${line_1#*] }";
# Iterate over the lines of the second input file
for line_2 in $(cat "${input_file_2}"); do
# Split the line on the first space
prefix_2="${line_2/ */}";
suffix_2="${line_2#*] }";
# If the prefixes agree, combine the suffixes and output the result
if [[ "${prefix_1}" = "${prefix_2}" ]]; then
echo "${prefix_1} ${suffix_1} X ${suffix_2}"
fi
done
done
次のように実行できます。
bash prefix-join.sh file-1.txt file-2.txt
サンプルデータの場合、Pythonスクリプトと同じ出力が生成されます。