adding a column to an output file with multiple entries
I have 2 files with the following data:
file 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
file 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
I want to have a single file that outputs the following:
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
It basically puts the return codes from file 1 into file 2 to but only at the matching time.
text-processing
add a comment |
I have 2 files with the following data:
file 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
file 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
I want to have a single file that outputs the following:
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
It basically puts the return codes from file 1 into file 2 to but only at the matching time.
text-processing
add a comment |
I have 2 files with the following data:
file 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
file 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
I want to have a single file that outputs the following:
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
It basically puts the return codes from file 1 into file 2 to but only at the matching time.
text-processing
I have 2 files with the following data:
file 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
file 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
I want to have a single file that outputs the following:
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
It basically puts the return codes from file 1 into file 2 to but only at the matching time.
text-processing
text-processing
edited 4 hours ago
Jeff Schaller
43.8k1161141
43.8k1161141
asked 4 hours ago
Vineeth ThomasVineeth Thomas
314
314
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Here is a Python script that should do what you want:
#!/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)
You would run it like this:
python prefix_join.py file-1.txt file-2.txt
On your example data, it produces the following output:
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507526%2fadding-a-column-to-an-output-file-with-multiple-entries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a Python script that should do what you want:
#!/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)
You would run it like this:
python prefix_join.py file-1.txt file-2.txt
On your example data, it produces the following output:
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
add a comment |
Here is a Python script that should do what you want:
#!/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)
You would run it like this:
python prefix_join.py file-1.txt file-2.txt
On your example data, it produces the following output:
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
add a comment |
Here is a Python script that should do what you want:
#!/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)
You would run it like this:
python prefix_join.py file-1.txt file-2.txt
On your example data, it produces the following output:
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
Here is a Python script that should do what you want:
#!/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)
You would run it like this:
python prefix_join.py file-1.txt file-2.txt
On your example data, it produces the following output:
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
edited 1 hour ago
answered 3 hours ago
igaligal
5,9411536
5,9411536
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f507526%2fadding-a-column-to-an-output-file-with-multiple-entries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown