The UNIX and Linux Forums  

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
Remove lines based on contents of another file bashshadow1979 Shell Programming and Scripting 4 03-25-2009 12:32 PM
Delete line in file based on data in another file earth_goddess Shell Programming and Scripting 1 12-29-2008 10:47 AM
Looking for a perl script (windows) to delete the contents of a file ntgobinath Shell Programming and Scripting 14 12-23-2008 01:08 PM
sh script that reads/writes based upon contents of a file rdudejr Shell Programming and Scripting 3 07-12-2006 11:38 AM
i want to delete a file based on existing file in a directory srivsn Shell Programming and Scripting 3 04-11-2006 05:38 AM

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

Join Date: Jun 2009
Posts: 74
How can i delete the contents of one file based on another file

Hi all,

I have two log files A and B. Where files A have 10 lines and file b has 5 lines.

Out of them 3 lines are common to both of them. I want to compare both the files and want to delete the common data from file A(file B should remain as it is)

example :

Code:
file A :
 
1
2
3
4
5
6
7
8
9
11
 
file B :
A
2
25
1
7
AND THE OUTPUT SHOULD BE

Code:
file A :
 
3
4
5
6
8
9
11
 
file B :
A
2
25
1
7
  #2 (permalink)  
Old 08-27-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Something like this :
Code:
awk 'NR==FNR { a[$0]=$0 ;next } { if($0 in a) {a[$0]="";}} END { for ( i in a) print a[i] }' file1 file2
  #3 (permalink)  
Old 08-27-2009
thanhdat's Avatar
thanhdat thanhdat is offline
Registered User
  
 

Join Date: Aug 2008
Location: Paris
Posts: 107
or use grep :
Code:
grep -v -f fileB fileA
  #4 (permalink)  
Old 08-27-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Yes ..Your right , no need to go for awk , if we can do in a simple grep statement.

My mind always strikes to work using awk ..
  #5 (permalink)  
Old 08-27-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
Hi Panyam/thanhdatI am unable to use grep -v -fas it is giving me the error below.
Code:
grep: illegal option -- fUsage: grep -hblcnsviw pattern file . . .
And the awk that has been specified is giving a blank space once the lines are deleted and the data is not coming in the same order as it is originally. For example, last line is coming inthe middle and vice versa. In my script, it should not change the order of the lines in the output.Please help. And let me know if you need more details.I am using SUN OS box.
  #6 (permalink)  
Old 08-28-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Not sure , why grep -v -f option is not working .

Try some thing like this :
Code:
awk 'NR==FNR { a[$0]=$0;b[++n]=$0;next } { if($0 in a) {a[$0]="";}} END { for (i=1;i<=n;i++) print a[b[i]] }' file1 file2 | sed '/^$/d'

Last edited by panyam; 08-28-2009 at 02:43 AM..
Reply

Bookmarks

Tags
compare, dlete, unix

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 03:29 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