Visit Our UNIX and Linux User Community


Compare


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Compare
# 1  
Old 01-21-2016
Compare

file1:
Code:
test1 123
test2 200
test3 300
test4 400
test5 500

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

my needed result:

Code:
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:
Code:
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:

Code:
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.

Last edited by Scrutinizer; 01-21-2016 at 03:44 PM.. Reason: Code tags, removed spurious formatting
# 2  
Old 01-21-2016
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 (https://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.
Code:
awk 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1] OFS $0}' Input_file1  Input_file2

Output will be as follows.
Code:
test1 123 test1 111
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-21-2016
Quote:
Originally Posted by loktamann
.
.
.
and tell me that test1 123 is not the same as test1 111
.
.
.
Try an extension to RavinderSingh13's proposal:
Code:
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

This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-21-2016
Fellas,
Thank you so much for a swift reply to my post.

Mr. Singh,
I thought this is code tag:
Code:
sdiff file1 file2 | grep -v ">" > file3
cat file3 | awk '{print $1,$2}' > source1
cat file3 | awk '{print $3,$4}' > target1
sdiff source1 target1


Last edited by Scrutinizer; 01-25-2016 at 03:43 PM.. Reason: code tags
# 5  
Old 01-21-2016
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 Smilie

https://www.youtube.com/watch?v=6SHhxh--JT4

Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-25-2016
RudiC,
your code works perfect, but please show me how to get extract table in file2 (table1 5) ?

my preference result:
Code:
test1 123 test1 111 different
test2 200 test2 200
test3 300 test3 300
test4 400 test4 400
test5 500 test5 500

extract table:
Code:
table1 5


Last edited by Scrutinizer; 01-25-2016 at 03:44 PM..
# 7  
Old 01-25-2016
Please be way more specific, unless awk '/table1 *5/' file2 satisfies you...
This User Gave Thanks to RudiC For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #582
Difficulty: Medium
A buffered input or output stream is one in which data that is read or written is actually stored in memory in a buffer until such a time as the buffer is flushed and the accumulated data is passed to or from the actual file.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 1, 1.0 and 1.00 ?

Hi, I have a file containing a list of codes, followed by a description, such as below: 008.0,ESCHERICHIA COLI 008.1,ARIZONA 008.2,AEROBACTER AEROGENES 008.6,ENTERITIS DUE TO SPECIFIED VIRUS 008.8,OTHER ORGANISM, NOT ELSEWHERE CLASSIFIED 008,INTESTINAL INFECTIONS DUE TO OTHER ORGANISMSI... (6 Replies)
Discussion started by: carlr
6 Replies

2. UNIX for Dummies Questions & Answers

Compare data - Match first column and compare second

Hi guys, looking for some help with a way to compare data in two files but with some conditions. example, File 1 consists of site1,10.1.1.1 site2,20.2.2.2 site3,30.3.3.3 File 2 contains site1,l0.1.1.1 site2,50.1.1.1 site3,30.3.3.3 site4,40.1.1.1 I want to be able to match the... (1 Reply)
Discussion started by: mutley2202
1 Replies

3. Shell Programming and Scripting

Need help on compare

Hi, I need your help with the below mentioned issue: I have 2 files say, file1.txt: id|no|cde a|4|7 b|3|2 c|8|8 d|8|9 file2.txt: id|no|cde a|4|6 b|2|2 c|8|8 (1 Reply)
Discussion started by: lkeswar
1 Replies

4. Shell Programming and Scripting

value compare

Hi, file contains only one row. END OF FILE. ROW COUNT: 8,9 We need to check the file contains exact string "END OF FILE. ROW COUNT: " if yes, get the 8,9 values then compare if both are equal print the "equal" if not "notequal". Thanks, (4 Replies)
Discussion started by: bmk
4 Replies

5. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

6. Shell Programming and Scripting

Compare

grep '^.......$' /usr/dict/words | cut -c1,2,3,5,6,7 This gives a list of 6 character strings, some are words some not. Any suggestions on how I can get rid of the ones that aren't words and print the ones that are? I have tried look with no luck yet. (1 Reply)
Discussion started by: kezkez
1 Replies

7. Shell Programming and Scripting

compare between the two dates

Hi all, How to check whether the given the two dates is minimal. example: Date 1 : 23-03-2008 with timestamp Date 2: 20-03-2008 With tmestamp I want to compare the twodates and which it gives the minimum date i wnat to get the output like this below output: the Date2 is... (1 Reply)
Discussion started by: balaji23_d
1 Replies

8. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

9. Shell Programming and Scripting

How to compare it ??

Hi how do you compare it in ksh ALINES=$(cat AFILE | wc -l) BFIRST=$(head -1 BFILE) I got ALINES=9 and BFRIST=records'9' I want 9=9 , how do you write BLINES=9 from records'9' so I can say ALINES==BLINES Thanks (4 Replies)
Discussion started by: sabercats
4 Replies

10. UNIX for Dummies Questions & Answers

Compare

Hi How can I compare two sendmail.cf from two different systems? We are using Tru64 Unix Thank you, Syed_45 (2 Replies)
Discussion started by: Syed_45
2 Replies

Featured Tech Videos