Help about comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help about comparison
# 1  
Old 01-27-2014
Help about comparison

Hello folks,

I have two files, which have usernames, I want to see the contents of file1.txt which is missing in file2.txt and another comparison file2.txt contents which is missing in file1.txt. please suggest.


Code:
file1.txt

user
u2
u8
a9
p9
p3
u4
z8
aaa
ahe
oktlo

Code:
file2.txt

myme
filep
u3
u4
z8

# 2  
Old 01-27-2014
Hello,

Here is the code which may help you.

Code:
awk 'NR==FNR {a[$1];next} !($1 in a) {print $0 " is NOT present in file2"}' file2 file1


Output will be as follows.

Code:
user is NOT present in file2
u2 is NOT present in file2
u8 is NOT present in file2
a9 is NOT present in file2
p9 is NOT present in file2
p3 is NOT present in file2
aaa is NOT present in file2
ahe is NOT present in file2
oktlo is NOT present in file2

Thanks,
R. Singh
# 3  
Old 01-27-2014
# 4  
Old 01-27-2014
Thanks, Is it possible while hardcoding "file2" in script we can automatically fetch by script itself?

Quote:
Originally Posted by RavinderSingh13
Hello,

Here is the code which may help you.

Code:
awk 'NR==FNR {a[$1];next} !($1 in a) {print $0 " is NOT present in file2"}' file2 file1


Output will be as follows.

Code:
user is NOT present in file2
u2 is NOT present in file2
u8 is NOT present in file2
a9 is NOT present in file2
p9 is NOT present in file2
p3 is NOT present in file2
aaa is NOT present in file2
ahe is NOT present in file2
oktlo is NOT present in file2

Thanks,
R. Singh
---------- Post updated at 11:41 AM ---------- Previous update was at 11:39 AM ----------

Quote:
Originally Posted by Akshay Hegde
It is wrong, not working for me.

---------- Post updated at 11:45 AM ---------- Previous update was at 11:41 AM ----------

I need to ask one question about this, in file1 i have 466 lines while file2 have 457 lines, so when i am comparing file1 with file2 so it should show 9 lines, while it is showing 12 lines why?





Quote:
Originally Posted by RavinderSingh13
Hello,

Here is the code which may help you.

Code:
awk 'NR==FNR {a[$1];next} !($1 in a) {print $0 " is NOT present in file2"}' file2 file1


Output will be as follows.

Code:
user is NOT present in file2
u2 is NOT present in file2
u8 is NOT present in file2
a9 is NOT present in file2
p9 is NOT present in file2
p3 is NOT present in file2
aaa is NOT present in file2
ahe is NOT present in file2
oktlo is NOT present in file2

Thanks,
R. Singh
# 5  
Old 01-27-2014
Quote:
Originally Posted by learnbash
Hello folks,

I have two files, which have usernames, I want to see the contents of file1.txt which is missing in file2.txt and another comparison file2.txt contents which is missing in file1.txt. please suggest.


Code:
file1.txt

user
u2
u8
a9
p9
p3
u4
z8
aaa
ahe
oktlo

Code:
file2.txt

myme
filep
u3
u4
z8

Quote:
Originally Posted by learnbash
Thanks, Is it possible while hardcoding "file2" in script we can automatically fetch by script itself?



---------- Post updated at 11:41 AM ---------- Previous update was at 11:39 AM ----------

Quote:
Originally Posted by Akshay Hegde View Post
Try :

Code:
$ grep -v -f <(sort file2) <(sort file1)


Following link might be useful
Two question: remove from the other variable or file to get another variable or file by Akshay Hegde - Shell Programming and Scripting - Unix Linux Forums
It is wrong, not working for me.

If you are trying on other than sample input you provided, you might get wrong result.

Code:
akshay@Aix:~/Desktop/s$ awk 'NR==FNR {a[$1];next} !($1 in a)' file2 file1 | sort
a9
aaa
ahe
oktlo
p3
p9
u2
u8
user

akshay@Aix:~/Desktop/s$ grep -v -f <(sort file2) <(sort file1)  | sort
a9
aaa
ahe
oktlo
p3
p9
u2
u8
user

# 6  
Old 01-27-2014
In file1 and file2, both contents usernames, i want to compare the username which is present in file1 but not present in file2.

---------- Post updated at 12:08 PM ---------- Previous update was at 11:55 AM ----------

Is it possible i can match the contents in both files?
# 7  
Old 01-27-2014
Quote:
Originally Posted by learnbash
In file1 and file2, both contents usernames, i want to compare the username which is present in file1 but not present in file2.

---------- Post updated at 12:08 PM ---------- Previous update was at 11:55 AM ----------

Is it possible i can match the contents in both files?

Code:
# Content in both file
$ awk 'NR==FNR {a[$1];next} ($1 in a)' file2 file1
u4
z8

# prints content of file1 which is not in file2 
$ awk 'NR==FNR {a[$1];next} !($1 in a)' file2 file1
user
u2
u8
a9
p9
p3
aaa
ahe
oktlo

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

date comparison

Hi friends, I would like to compare two dates in an IF statement. This is what I am trying, but it doesn't work. date=20120122 minus=6 if ; then ... fi what would the IF clause looks like? Thanks! :) (5 Replies)
Discussion started by: kokoro
5 Replies

2. UNIX for Dummies Questions & Answers

comparison

hi guys i need a program that can compare a value read from a com-port and one from the terminal. can somebody help me??? using linux kernel 2.6.14-M5 can only use standard function in sh and bash... (5 Replies)
Discussion started by: metal005
5 Replies

3. Shell Programming and Scripting

$((...)) and $[...] comparison

Does $((mathematical expression)) and $ mean the same? (7 Replies)
Discussion started by: proactiveaditya
7 Replies

4. Shell Programming and Scripting

String Comparison

Is there a way to compare the permission string of two files and output the string if they match? For ex: -rw-r--r-- 1 user newuser 0 2009-03-12 16:45 file2 -rw-r--r-- 1 user newuser 0 2009-03-12 16:46 fileone output: -rw-r--r-- If they don't match output will be just... (3 Replies)
Discussion started by: squardius
3 Replies

5. UNIX for Dummies Questions & Answers

Timestamp comparison

How do I compare 2 timestamps (ie... if 2008-02-13 10:48:58.502075 gt 2008-12-15 16:00:00.000000) (4 Replies)
Discussion started by: auzark
4 Replies

6. UNIX for Dummies Questions & Answers

Help with array comparison

I have an array @name with the below contents (index 7 and 8 are names): 1|1|2|2|I|2|0|DUNN|LACY|||||| 2|2|2|2|I|2|0|KOFE|ROGER|||||| 3|3|2|2|A|2|0|KOFOED|ROBERT|||||| 3|4|2|2|A|2|0|KOFOED|ROBERT|||||| 3|5|2|2|A|2|0|KOFOED|ROBERT|||||| 2|7|2|2|I|2|0|WILLIAMSON|JAMES||||||... (4 Replies)
Discussion started by: ChicagoBlues
4 Replies

7. Shell Programming and Scripting

list comparison

what is the easy way to compare checksums of sereral paramters by script: for example: #im creating a file containing a list of checksum parameters cksum /opt/LGTOaam51/data_source_types/UX_File_System/* > t1 #and another one cksum /opt/LGTOaam51/data_source_types/UX_File_System2/* > t2 ... (2 Replies)
Discussion started by: modcan
2 Replies

8. Shell Programming and Scripting

file comparison

hi I have 2 files to comapre ,in file a sible column it is numbers,in file b2 numbers and other values with coma separated. i want compare numbers in file a with file b,and the out put put should be in C with numbers in both file a and b along with other columns of file b. i used folowing... (7 Replies)
Discussion started by: satish.res
7 Replies

9. Shell Programming and Scripting

need some help..Comparison

I need some help which would probably be for most of you a simple script. I need to read in the data from a .dat file and then compare avg to see who is the highest avg. Here is my script so far. #!/bin/ksh #reading in the data from lab3.dat filename=$1 while read name o1 o2 o3 o4 o5 o6... (0 Replies)
Discussion started by: bluesilo
0 Replies

10. Filesystems, Disks and Memory

comparison

can anyone point me to a comparison of *nix file systems ? i think i prefer a journalling fs but i would like to see a comparison between several fs's before i make up my mind (2 Replies)
Discussion started by: cnf
2 Replies
Login or Register to Ask a Question