Linux: how to extract rows of certain IDs and the IDs are writtein in another files?
Clash Royale CLAN TAG#URR8PPP
I have a csv file of IDs.
1
3
8
I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
I want to make this file
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
linux text-processing
add a comment |
I have a csv file of IDs.
1
3
8
I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
I want to make this file
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
linux text-processing
If it wasn't for numbers, thejoin
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
– A.B
Dec 24 '18 at 16:48
add a comment |
I have a csv file of IDs.
1
3
8
I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
I want to make this file
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
linux text-processing
I have a csv file of IDs.
1
3
8
I have another file of information, not only for the IDs, but also others. If I want to extract the information only of the IDs, how to do it?
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
I want to make this file
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
linux text-processing
linux text-processing
edited Dec 26 '18 at 23:15
jimmij
30.9k870105
30.9k870105
asked Dec 24 '18 at 4:22
user10345633
111
111
If it wasn't for numbers, thejoin
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
– A.B
Dec 24 '18 at 16:48
add a comment |
If it wasn't for numbers, thejoin
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically
– A.B
Dec 24 '18 at 16:48
If it wasn't for numbers, the
join
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically– A.B
Dec 24 '18 at 16:48
If it wasn't for numbers, the
join
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically– A.B
Dec 24 '18 at 16:48
add a comment |
4 Answers
4
active
oldest
votes
If the files are space-delimited and have Unix-style line endings:
cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
add a comment |
If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):
f1:
01
03
08
f2:
01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7
Then:
$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
add a comment |
I would use awk
:
awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2
where your IDs are in file1 and the rest in file2. The output:
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
add a comment |
using Miller you could do in this way
mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv
to obtain
1,96,283,4
3,57,294,0
3,24,284,0
8,19,239,78
8,18,289,90
joinInputOne.csv is
1
3
8
and
joinInputTwo.csv is
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
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%2f490711%2flinux-how-to-extract-rows-of-certain-ids-and-the-ids-are-writtein-in-another-fi%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
If the files are space-delimited and have Unix-style line endings:
cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
add a comment |
If the files are space-delimited and have Unix-style line endings:
cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
add a comment |
If the files are space-delimited and have Unix-style line endings:
cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt
If the files are space-delimited and have Unix-style line endings:
cat file1.txt | xargs -I '' -n 1 grep '^ ' file2.txt
edited Dec 24 '18 at 12:13
steve
13.9k22452
13.9k22452
answered Dec 24 '18 at 11:54
artem
111
111
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
add a comment |
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
1
1
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
With 4 space before your command you can activate the code block feature which is better readable.
– ploth
Dec 24 '18 at 11:58
add a comment |
If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):
f1:
01
03
08
f2:
01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7
Then:
$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
add a comment |
If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):
f1:
01
03
08
f2:
01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7
Then:
$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
add a comment |
If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):
f1:
01
03
08
f2:
01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7
Then:
$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90
If you can homogenize the first field in each file (eg by adding a leading '0'), you can use join(1):
f1:
01
03
08
f2:
01 96 283 4
02 87 918 6
02 49 208 5
03 57 294 0
03 24 284 0
04 28 345 290
05 23 234 209
06 98 245 02
07 18 329 89
08 19 239 78
08 18 289 90
09 28 390 09
09 19 238 09
10 23 899 7
Then:
$ join f f2
01 96 283 4
03 57 294 0
03 24 284 0
08 19 239 78
08 18 289 90
answered Dec 24 '18 at 4:37
wef
993
993
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
add a comment |
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
Thank you so much for your prompt reply. I tried with join ids.txt table.txt > output.txt, but it does not work well. Any ideas? Thank you!
– user10345633
Dec 24 '18 at 5:24
add a comment |
I would use awk
:
awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2
where your IDs are in file1 and the rest in file2. The output:
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
add a comment |
I would use awk
:
awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2
where your IDs are in file1 and the rest in file2. The output:
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
add a comment |
I would use awk
:
awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2
where your IDs are in file1 and the rest in file2. The output:
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
I would use awk
:
awk 'NR==FNRa[$0]++;nexta[$1]' file1 file2
where your IDs are in file1 and the rest in file2. The output:
1 96 283 4
3 57 294 0
3 24 284 0
8 19 239 78
8 18 289 90
answered Dec 26 '18 at 23:14
jimmij
30.9k870105
30.9k870105
add a comment |
add a comment |
using Miller you could do in this way
mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv
to obtain
1,96,283,4
3,57,294,0
3,24,284,0
8,19,239,78
8,18,289,90
joinInputOne.csv is
1
3
8
and
joinInputTwo.csv is
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
add a comment |
using Miller you could do in this way
mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv
to obtain
1,96,283,4
3,57,294,0
3,24,284,0
8,19,239,78
8,18,289,90
joinInputOne.csv is
1
3
8
and
joinInputTwo.csv is
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
add a comment |
using Miller you could do in this way
mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv
to obtain
1,96,283,4
3,57,294,0
3,24,284,0
8,19,239,78
8,18,289,90
joinInputOne.csv is
1
3
8
and
joinInputTwo.csv is
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
using Miller you could do in this way
mlr --implicit-csv-header --headerless-csv-output --ifs " " --csv join -j 1 -r 1 -f joinInputOne.csv joinInputTwo.csv
to obtain
1,96,283,4
3,57,294,0
3,24,284,0
8,19,239,78
8,18,289,90
joinInputOne.csv is
1
3
8
and
joinInputTwo.csv is
1 96 283 4
2 87 918 6
2 49 208 5
3 57 294 0
3 24 284 0
4 28 345 290
5 23 234 209
6 98 245 02
7 18 329 89
8 19 239 78
8 18 289 90
9 28 390 09
9 19 238 09
10 23 899 7
answered Dec 31 '18 at 8:23
aborruso
1466
1466
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f490711%2flinux-how-to-extract-rows-of-certain-ids-and-the-ids-are-writtein-in-another-fi%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
If it wasn't for numbers, the
join
command (join -j 1 id.csv informations.csv
) would have done it. As it is more tinkering or other tools might be needed, because I don't find a way to have it sort numerically– A.B
Dec 24 '18 at 16:48