Visit Our UNIX and Linux User Community


How can i delete the contents of one file based on another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i delete the contents of one file based on another file
# 1  
Old 08-27-2009
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  
Old 08-27-2009
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  
Old 08-27-2009
or use grep :
Code:
grep -v -f fileB fileA

# 4  
Old 08-27-2009
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 .. Smilie Smilie
# 5  
Old 08-27-2009
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  
Old 08-28-2009
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 03:43 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #452
Difficulty: Medium
San Francisco has a Linux-based traffic controller computer that meets the latest Advanced Transportation Controller (ATC) standards.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

File comparison based on contents

Hi I have 2 files 1.del ---- 1,2,3,4,5 1,2,3,4,4 1,1,1,1,2 2.del ---- 1,2,3,4,5 1, 1,2,3,4,4 1,1,1,1,2 I need to compare the above two files in unix, as in the output should only tell the difference in contents as I should get only the line 1 ( from 2.del) , rest all lines are... (4 Replies)
Discussion started by: Ethen561
4 Replies

3. UNIX for Dummies Questions & Answers

find lines in another file based on contents in a second file

Hello, I have a file with tab delimited columns like: File1 A 2 C R F 4 D Q C 9 A B ...... I want to grep out the lines in a second file, File2, corresponding to each line in File1 Can I do this: while read a b c d do grep '$a\t$b\t$c\t$d' File2 >>... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

4. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

5. UNIX for Dummies Questions & Answers

count values based on contents of another file

Hello, I have two files as shown below: test1 678 679 689 690 710 test2 1 678 654 800 676 791 689 900 I want to get a count of lines from test2 whose columns bound the values in test1 I tried running the code below; however am getting wrong results. (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

6. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

7. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

8. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies

9. Shell Programming and Scripting

Looking for a perl script (windows) to delete the contents of a file

Hi All, Im having a file named logserver.txt. I want a perl script to take a backup of that file, along with the datestamp; move the file to a different location or empty the contents of the file after backup. Remember, the file gets generated when the related service starts. My condition is... (14 Replies)
Discussion started by: ntgobinath
14 Replies

Featured Tech Videos