UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 6th, 2008, 02:16 PM
ydeewana ydeewana is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 2 ydeewana User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 26 sec
Reputation Power: 0
Compare 2 files

Hi, I want to compare file1 (file1_column 1) with file2 (file2_column1), if the file2_column1 match in file1 (file1_column 1) then print that line from file2 to new file (file3).

second, I want only unique values(I can use (file3_column_1) in file3 or create new file (file 4).

File1:

ID111 Test1
IN111 test2
IP112 test5
NC113 test9

File2:
NC113 test9
NC113 test9
IN111 test2
PC123 test22

Output:
NC113 test9
IN111 test2

Thanks.

Reply With Quote
  #2  
Old February 6th, 2008, 05:16 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 6 h 19 m 24 sec
Reputation Power: 48
One way using awk
Code:
awk ' FILENAME=="file1" { arr[$1]=$0 }
        FILENAME=="file2" { if(arr($1]) print arr[$1]}
       ' file1 file2 | sort -u > file3 

Reply With Quote
  #3  
Old February 7th, 2008, 03:14 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,102 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 21 h 8 m
Reputation Power: 9
Quote:
Originally Posted by jim mcnamara
One way using awk
Code:
awk ' FILENAME=="file1" { arr[$1]=$0 }
        FILENAME=="file2" { if(arr($1]) print arr[$1]}
       ' file1 file2 | sort -u > file3 


read man sort && comm
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #4  
Old February 8th, 2008, 11:20 AM
ydeewana ydeewana is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 2 ydeewana User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 46 m 26 sec
Reputation Power: 0
Hi Jim, Thanks for you help.

I run the script but getting an error. here is my scripts.

#!/bin/ksh
EXPORT_HOME=/export/home/Test/scripts/
EXPORT_HOME_SCRIPTS=$EXPORT_HOME/Yash
cd $EXPORT_HOME_SCRIPTS

FILE1=user_id_list; export FILE1
FILE2=/logs/active_id.log; export FILE2

awk -F 'FILENAME=="FILE1" { arr[$1]=$0 } FILENAME=="FILE2" { if(arr($1]) print arr[$1]} 'FILE1 FILE2 | so
rt -u} > file3

exit 0

error message.

awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1


is there something am i missing here?

Reply With Quote
  #5  
Old February 9th, 2008, 02:01 PM
etienne141's Avatar
etienne141 etienne141 is offline
Paris est magique!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: France!
Posts: 346 etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level)etienne141 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 5 Days 2 h 12 m 41 sec
Reputation Power: 41
Send a message via ICQ to etienne141 Send a message via MSN to etienne141
What about:

Code:
for line in `more file1`; do grep $line file2 | sort -u >> file3; done
__________________
UNIX shells are so cool!
etienne:~ > %blow
fg: %blow: no such job


There are 10 kind of people:
- those who know binary
- those who don't.

Reply With Quote
  #6  
Old February 15th, 2008, 11:10 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,102 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 21 h 8 m
Reputation Power: 9
a) sort your files
b) read man pages of comm
c) you got it

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Compare 2 files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT