Matching contents of one file with another and returning second columnLooping through the content of a file in BashFast way of finding lines in one file that are not in another?awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second fileDisplay only lines in which 1 column is equal to another, and a second column is in a range in AWK and BashHow to compare multiple columns in two files and retrieve the corresponding value from another column if match foundCompare columns in two text files and match linesCompare two columns of two files and display the third column with input if it matching of not in unixReplace value in file with value from another file based on matching string (otherwise: skip)Filter a file based on values in columns of another fileCompare first column of one file with the first column of second and print associated column of each if there was a match
How to write sign "|" (or) in LaTeX?
Can there be absolute velocity?
Do empty drive bays need to be filled?
How do you play "tenth" chords on the guitar?
What is the Leave No Trace way to dispose of coffee grounds?
How to avoid typing 'git' at the begining of every Git command
That's not my X, its Y is too Z
Please help me with the correction in table column width and the errors. You can give your suggestions according to your own
Why is Na5 not played in this line of the French Defense, Advance Variation?
Use 1 9 6 2 in this order to make 75
What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?
If I had a daughter who (is/were/was) cute, I would be very happy
So a part of my house disappeared... But not because of a chunk resetting
Why are MBA programs closing in the United States?
Diatonic chords of a pentatonic vs blues scale?
Tikz-cd diagram arrow passing under a node - not crossing it
What is the reason for setting flaps 1 on the ground at high temperatures?
Make Gimbap cutter
Is it okay to have a sequel start immediately after the end of the first book?
Difference between prepositions in "...killed during/in the war"
Was planting UN flag on Moon ever discussed?
To what extent do precedents in Westminster systems apply in other countries that use it?
I've been given a project I can't complete, what should I do?
Why isn't Bash trap working if output is redirected to stdout?
Matching contents of one file with another and returning second column
Looping through the content of a file in BashFast way of finding lines in one file that are not in another?awk compare 2 files, print match and nonmatch lines;3rd column of first file and 2nd column of second fileDisplay only lines in which 1 column is equal to another, and a second column is in a range in AWK and BashHow to compare multiple columns in two files and retrieve the corresponding value from another column if match foundCompare columns in two text files and match linesCompare two columns of two files and display the third column with input if it matching of not in unixReplace value in file with value from another file based on matching string (otherwise: skip)Filter a file based on values in columns of another fileCompare first column of one file with the first column of second and print associated column of each if there was a match
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
So I have two txt files
file1.txt
s
j
z
z
e
and file2.txt
s h
f a
j e
k m
z l
d p
e o
and what I want to do is match the first letter of file1 with the first letter of file 2 and return the second column of file 2. so for example excepted output would be
h
e
l
l
o
I'm trying to use join file1.txt file2.txt but that just prints out the entire second file. not sure how to fix this. Thank you.
bash unix
add a comment |
So I have two txt files
file1.txt
s
j
z
z
e
and file2.txt
s h
f a
j e
k m
z l
d p
e o
and what I want to do is match the first letter of file1 with the first letter of file 2 and return the second column of file 2. so for example excepted output would be
h
e
l
l
o
I'm trying to use join file1.txt file2.txt but that just prints out the entire second file. not sure how to fix this. Thank you.
bash unix
1
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57
add a comment |
So I have two txt files
file1.txt
s
j
z
z
e
and file2.txt
s h
f a
j e
k m
z l
d p
e o
and what I want to do is match the first letter of file1 with the first letter of file 2 and return the second column of file 2. so for example excepted output would be
h
e
l
l
o
I'm trying to use join file1.txt file2.txt but that just prints out the entire second file. not sure how to fix this. Thank you.
bash unix
So I have two txt files
file1.txt
s
j
z
z
e
and file2.txt
s h
f a
j e
k m
z l
d p
e o
and what I want to do is match the first letter of file1 with the first letter of file 2 and return the second column of file 2. so for example excepted output would be
h
e
l
l
o
I'm trying to use join file1.txt file2.txt but that just prints out the entire second file. not sure how to fix this. Thank you.
bash unix
bash unix
asked Mar 24 at 21:41
serenitynow13serenitynow13
233
233
1
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57
add a comment |
1
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57
1
1
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57
add a comment |
1 Answer
1
active
oldest
votes
This is an awk classic:
$ awk 'NR==FNRa[$1]=$2;nextprint a[$1]' file2 file1
h
e
l
l
o
Explained:
$ awk '
NR==FNR # processing file2
a[$1]=$2 # hash records, first field as key, second is the value
next
# second file
print a[$1] # output, change the record with related, stored one
' file2 file1
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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%2fstackoverflow.com%2fquestions%2f55328846%2fmatching-contents-of-one-file-with-another-and-returning-second-column%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
This is an awk classic:
$ awk 'NR==FNRa[$1]=$2;nextprint a[$1]' file2 file1
h
e
l
l
o
Explained:
$ awk '
NR==FNR # processing file2
a[$1]=$2 # hash records, first field as key, second is the value
next
# second file
print a[$1] # output, change the record with related, stored one
' file2 file1
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
add a comment |
This is an awk classic:
$ awk 'NR==FNRa[$1]=$2;nextprint a[$1]' file2 file1
h
e
l
l
o
Explained:
$ awk '
NR==FNR # processing file2
a[$1]=$2 # hash records, first field as key, second is the value
next
# second file
print a[$1] # output, change the record with related, stored one
' file2 file1
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
add a comment |
This is an awk classic:
$ awk 'NR==FNRa[$1]=$2;nextprint a[$1]' file2 file1
h
e
l
l
o
Explained:
$ awk '
NR==FNR # processing file2
a[$1]=$2 # hash records, first field as key, second is the value
next
# second file
print a[$1] # output, change the record with related, stored one
' file2 file1
This is an awk classic:
$ awk 'NR==FNRa[$1]=$2;nextprint a[$1]' file2 file1
h
e
l
l
o
Explained:
$ awk '
NR==FNR # processing file2
a[$1]=$2 # hash records, first field as key, second is the value
next
# second file
print a[$1] # output, change the record with related, stored one
' file2 file1
answered Mar 24 at 21:50
James BrownJames Brown
21.3k42138
21.3k42138
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
add a comment |
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
1
1
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
All right, uh, all right this is uh, this is an oldie, but uh, well, it's an oldie where I come from, uh. All right, guys, uh, listen. This is a blues riff in 'B', watch me for the changes, and try and keep up, okay.:
– James Brown
Mar 24 at 22:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f55328846%2fmatching-contents-of-one-file-with-another-and-returning-second-column%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
1
Does the order of rows have to be maintained?
– Cyrus
Mar 24 at 21:50
yes, or else I won't be able to get the correct message ("hello")
– serenitynow13
Mar 24 at 21:57