Add prefix to each lines of file from other file
I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.
cat file1.txt
aa
bb
cc
cat file2.txt
site.com
site2.com
site3.com
Expected result will look like:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
linux command-line sed
add a comment |
I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.
cat file1.txt
aa
bb
cc
cat file2.txt
site.com
site2.com
site3.com
Expected result will look like:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
linux command-line sed
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42
add a comment |
I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.
cat file1.txt
aa
bb
cc
cat file2.txt
site.com
site2.com
site3.com
Expected result will look like:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
linux command-line sed
I have two files named file1.txt and file2.txt, I'm trying to add the prefix to file2.txt from file1.txt in a manner so I can pipe the results to the next utility I'm using.
cat file1.txt
aa
bb
cc
cat file2.txt
site.com
site2.com
site3.com
Expected result will look like:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
linux command-line sed
linux command-line sed
asked Jan 12 at 10:00
SamSam
1
1
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42
add a comment |
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42
add a comment |
2 Answers
2
active
oldest
votes
With bash:
while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt
or
join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'
Output:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
add a comment |
The easy part is that you can use paste
to merge the lines of your files. Its -d
option lets you choose a delimiter (here, a .
).
The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:
paste -d '.'
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))
Where we:
- Combine
yes
andhead
to make ased
script that will print each line offile1.txt
a number of times equal to the number of lines infile2.txt
; - Combine
yes
andhead
to makecat
printfile2.txt
a number of times equal to the number of lines infile1.txt
; - Use
paste
to merge each pair of lines printed by the two process substitutions (<(...)
), separated by a.
.
Of course you can pipe the result into other commands.
Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try
while ... do ... done <file | cat -
For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc
if you use bash
).
An example, here using loops to minimize the need for external tools:
function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}
Sample usage:
$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f1393467%2fadd-prefix-to-each-lines-of-file-from-other-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With bash:
while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt
or
join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'
Output:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
add a comment |
With bash:
while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt
or
join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'
Output:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
add a comment |
With bash:
while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt
or
join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'
Output:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
With bash:
while IFS= read -r line1; do while IFS= read -r line2; do echo "$line1.$line2"; done <file2.txt; done <file1.txt
or
join -j 64 <(sort file1.txt) <(sort file2.txt) -o 1.1,2.1 | sed 's/ /./'
Output:
aa.site.com
aa.site2.com
aa.site3.com
bb.site.com
bb.site2.com
bb.site3.com
cc.site.com
cc.site2.com
cc.site3.com
edited Jan 12 at 11:13
answered Jan 12 at 11:08
CyrusCyrus
3,79611024
3,79611024
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
add a comment |
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
Thank you for your answer @Cyrus, it works as expected.
– Sam
Jan 13 at 12:09
add a comment |
The easy part is that you can use paste
to merge the lines of your files. Its -d
option lets you choose a delimiter (here, a .
).
The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:
paste -d '.'
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))
Where we:
- Combine
yes
andhead
to make ased
script that will print each line offile1.txt
a number of times equal to the number of lines infile2.txt
; - Combine
yes
andhead
to makecat
printfile2.txt
a number of times equal to the number of lines infile1.txt
; - Use
paste
to merge each pair of lines printed by the two process substitutions (<(...)
), separated by a.
.
Of course you can pipe the result into other commands.
Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try
while ... do ... done <file | cat -
For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc
if you use bash
).
An example, here using loops to minimize the need for external tools:
function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}
Sample usage:
$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com
add a comment |
The easy part is that you can use paste
to merge the lines of your files. Its -d
option lets you choose a delimiter (here, a .
).
The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:
paste -d '.'
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))
Where we:
- Combine
yes
andhead
to make ased
script that will print each line offile1.txt
a number of times equal to the number of lines infile2.txt
; - Combine
yes
andhead
to makecat
printfile2.txt
a number of times equal to the number of lines infile1.txt
; - Use
paste
to merge each pair of lines printed by the two process substitutions (<(...)
), separated by a.
.
Of course you can pipe the result into other commands.
Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try
while ... do ... done <file | cat -
For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc
if you use bash
).
An example, here using loops to minimize the need for external tools:
function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}
Sample usage:
$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com
add a comment |
The easy part is that you can use paste
to merge the lines of your files. Its -d
option lets you choose a delimiter (here, a .
).
The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:
paste -d '.'
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))
Where we:
- Combine
yes
andhead
to make ased
script that will print each line offile1.txt
a number of times equal to the number of lines infile2.txt
; - Combine
yes
andhead
to makecat
printfile2.txt
a number of times equal to the number of lines infile1.txt
; - Use
paste
to merge each pair of lines printed by the two process substitutions (<(...)
), separated by a.
.
Of course you can pipe the result into other commands.
Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try
while ... do ... done <file | cat -
For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc
if you use bash
).
An example, here using loops to minimize the need for external tools:
function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}
Sample usage:
$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com
The easy part is that you can use paste
to merge the lines of your files. Its -d
option lets you choose a delimiter (here, a .
).
The hard part here is the Cartesian product. Borrowing from this answer on SO, we can come up with a command like this:
paste -d '.'
<(sed -n "$(yes 'p;' | head -n $(wc -l <file2.txt) )" file1.txt)
<(cat $(yes 'file2.txt' | head -n $(wc -l <file1.txt)))
Where we:
- Combine
yes
andhead
to make ased
script that will print each line offile1.txt
a number of times equal to the number of lines infile2.txt
; - Combine
yes
andhead
to makecat
printfile2.txt
a number of times equal to the number of lines infile1.txt
; - Use
paste
to merge each pair of lines printed by the two process substitutions (<(...)
), separated by a.
.
Of course you can pipe the result into other commands.
Also, note that you can always pipe the output of commands, even if they are in a loop, as in this other answer you have. E.g. try
while ... do ... done <file | cat -
For convenience, you can define a function and make it available to your environment (e.g. defining it in your .bashrc
if you use bash
).
An example, here using loops to minimize the need for external tools:
function cart_prod () {
while IFS= read -r line1; do
while IFS= read -r line2; do
printf '%s.%sn' "$line1" "$line2"
done <"$2"
done <"$1"
}
Sample usage:
$ cart_prod file1.txt file2.txt | sort -r
cc.site.com
cc.site3.com
cc.site2.com
bb.site.com
bb.site3.com
bb.site2.com
aa.site.com
aa.site3.com
aa.site2.com
edited Jan 13 at 11:32
answered Jan 12 at 11:08
fra-sanfra-san
22614
22614
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f1393467%2fadd-prefix-to-each-lines-of-file-from-other-file%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
Please take a look at: What should I do when someone answers my question?
– Cyrus
Jan 13 at 12:42