Compare

file1:

test1 123
test2 200
test3 300
test4 400
test5 500

file2:

table1 5
test1 111
test2 200
table2 100
test3 300
test4 400
test5 500

my needed result:

test1 123 test1 111
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500

and tell me that test1 123 is not the same as test1 111

I had done this:

sdiff file1 file2 | grep -v ">" > file3
cat file3 | awk '{print $1,$2}' > source1
cat file3 | awk '{print $3,$4}' > target1
sdiff source1 target1

and my result:

test1 123 | | table1
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500

my co-work told me to use vlookup, but I need to script it out.
Thank you so much in advance for your help.

Hello loktamann,

Welcome to forums, hope you will enjoy learning/sharing knowledge/experiences here with us. Request you to please use code tags as per forum rules for codes/Inputs/commands which you are using into your posts. Following is the forum rules link (http://www.unix.com/misc.php?do=cfrules\) which you could through once and can see how important is code tags here to maintain the forums standards because it makes our life really easy to help/advice/solve/guide everyone. Coming to your question you could once use Search option present in the command bar too for checking these kind of solutions as this is one of the common asked question here, following may help you in same though.

awk 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1] OFS $0}' Input_file1  Input_file2

Output will be as follows.

test1 123 test1 111
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500
 

Thanks,
R. Singh

1 Like

Try an extension to RavinderSingh13's proposal:

awk 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1], $0, A[$1]!=$0?"different":""}' file1  file2
test1 123 test1 111 different
test2 200 test2 200 
1 Like

Fellas,
Thank you so much for a swift reply to my post.

Mr. Singh,
I thought this is code tag:

sdiff file1 file2 | grep -v ">" > file3
cat file3 | awk '{print $1,$2}' > source1
cat file3 | awk '{print $3,$4}' > target1
sdiff source1 target1

Your welcome loktamann. Let me show you 2 ways to use code tags.
1st: While replying to any post or while opening a new THREAD etc, in TITLE bar you will see a button where it will be written co then down de.
2nd: Following URL on youtube will be very helpful for you to see how we use code tags :b:

The UNIX and Linux Forums: Using Code Tags - YouTube

Hope this helps.

Thanks,
R. Singh

1 Like

RudiC,
your code works perfect, but please show me how to get extract table in file2 (table1 5) ?

my preference result:

test1 123 test1 111 different
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500

extract table:

table1 5

Please be way more specific, unless awk '/table1 *5/' file2 satisfies you...

1 Like

Personally, I'd suggest man join . This is the exemplary usage of this tool.

I hope this helps.

bakunin

i used while and grep -v to get unwanted tables. Again, thank you so much for helping.