2

So yeah im trying to match file1 that contains email to file2 that cointains email colons address, how do i go on bout doing that?

tried awk 'FNR==NR{a[$1]=$0; next}{print a[$1] $0}' but idk what im doing wrong

file1:

[email protected]
[email protected]
[email protected]

file2:

[email protected]:addressotest
[email protected]:clubbingson
[email protected]:addresso2

output:

[email protected]:addresso2
[email protected]:addressotest
6
  • Show your efforts and then get the answer ! Commented Feb 2, 2018 at 7:05
  • i've tried awk 'FNR==NR{a[$1]=$0; next}{print a[$1] $0}' file1 file2 but does not work Commented Feb 2, 2018 at 7:06
  • Post some sample data with expected output, please. We don't enjoy the quessing game. Commented Feb 2, 2018 at 7:07
  • 1
    sorry about that, I added data Commented Feb 2, 2018 at 7:09
  • 1
    You need to set the field separator for file2. Try awk -F: [your code here] file1 file2. Commented Feb 2, 2018 at 7:11

3 Answers 3

1

Following awk may help you in same.

awk 'FNR==NR{a[$0];next} ($1 in a)' FILE_1 FS=":" FILE_2
Sign up to request clarification or add additional context in comments.

6 Comments

@JohnnyB, it worked for me, what error are you getting?
Im not getting any errors, the command responds as if no matches were found
@JohnnyB, did you run the exact command? It is working fine for me.
I tried it with the examples I provided, it works, but when I try it with my actual files, it does not work. (the files I have are big files)
Then the files probably differ from samples somehow. How big are the files?
|
1

join with presorting input files

$ join -t: <(sort file1) <(sort file2)

[email protected]:addressotest
[email protected]:addresso2

Comments

0

Hey why going for a awk solution when you can simply use the following join command:

join -t':' file 1 file2

where join as its names indicate is just a file joining command and you chose the field separator and usually the input columns and output to display (here not necessary)

Tested:

$more file{1,2}
::::::::::::::
file1
::::::::::::::
[email protected]
[email protected]
[email protected]
::::::::::::::
file2
::::::::::::::
[email protected]:addressotest
[email protected]:clubbingson
[email protected]:addresso2

$join -t':' file1 file2
[email protected]:addressotest
[email protected]:addresso2

If you need to sort the output as well, change the command into:

join -t':' file 1 file2 | sort -t":" -k1 

or

join -t':' file 1 file2 | sort -t":" -k2

depending on which column you want to sort upon (eventually add the -r option to sort in reverse order.

join -t':' file 1 file2 | sort -t":" -k1 -r

or

join -t':' file 1 file2 | sort -t":" -k2 -r

4 Comments

I've tried this, but I get join: file2.txt: 4 is not sorted: [email protected]:addressotest join file1.txt:2: is not sorted: [email protected]
what is your sorting criteria?
@JohnnyB I have edited my answer please have a look! ;-)
you have to sort input files!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.