The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare two files for matching in solaris amir07 Shell Programming and Scripting 8 01-02-2009 06:40 PM
Compare two files and remove all the contents of one file from another royalibrahim Shell Programming and Scripting 3 11-18-2008 12:55 AM
compare files by lines and columns giviut Shell Programming and Scripting 4 01-17-2008 06:00 AM
Trying to compare lines in 2 files brdholman Shell Programming and Scripting 2 09-20-2007 07:46 AM
Find matching lines between 2 files jojojmac5 UNIX for Dummies Questions & Answers 5 01-18-2007 01:06 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-19-2009
shellscripter shellscripter is offline
Registered User
  
 

Join Date: Oct 2008
Location: chennai
Posts: 16
compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files
  #2 (permalink)  
Old 06-19-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Code:
awk '{arr[$0]++} END {for (i in arr) {if (arr[i]>1) {print $0} }} ' file1 file2 > dupfile
awk 'FILENAME=="dupfile" {arr[$0]++}
       FILENAME=="file1" {if ($0 in arr) {next} else {print $0 >"newfile1"}}
       FILENAME=="file2" {if ($0 in arr) {next} else {print $0 >"newfile2"} }' dupfile file1 file2
Not tested.
  #3 (permalink)  
Old 06-19-2009
King Kalyan King Kalyan is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 12
Try this...

Code:
 
awk '{ if (FNR==NR) {arr[$0]++;next}
if ($0 in arr) { arr[$0]--; if (arr[$0] == 0) delete arr[$0]; next}
{print $0 >"newfile2"} } 
END {for (i in arr) {print i >"newfile1"} } ' file1 file2
Jim: The code you have given has potential to remove duplicate lines in one file even though no matching record present in other file because those lines has multiple instances in a single file.

Last edited by King Kalyan; 06-19-2009 at 04:06 PM.. Reason: Corrected the code for duplicates also...
  #4 (permalink)  
Old 06-20-2009
kingpeejay kingpeejay is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 66
I also have this kind of problem. The code above deletes the lines which are alike on the two files. How can i do this, on a separate file?
File 3:
#from first file
a3px lm4 xpm
0wmp jga1 wmp6

#from second file
a3px lm4 xpj
0xmp lm4 xpm

thanks a lot!
  #5 (permalink)  
Old 06-20-2009
scottn scottn is offline Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,042
maybe?

Code:
awk '! LINE[$0] { print; LINE[$0] = $0}' in_file
Reply

Bookmarks

Tags
file comparison

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0