Compare 3 files, delete data equals.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 3 files, delete data equals.
# 1  
Old 07-14-2010
Question Compare 3 files, delete data equals.

Hi, i have a problem,

I have three files, file_1, File_2 file_3 and I need to compare the data with file_3 file_1, data that are equal to file_3 file_1 should be deleted, file_1 receive data and file_2 file_3.

Ex:
file_1
Code:
374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00
789502, 2008, Eduardo Calmon, Analista Sênior, U$8.000,00
953585, 2010, Carolina Sant Ana, Analista Plena, U$10.000,00


File_2
Code:
8795254,2001, Frederico Marlon, Técnico de Sistemas, U$3.500,00
7989653, 2007, Jamile Cupertino, Analista Junior, U$6.000,00
5823647, 2008, Célia Cupertino, Gerente, U$11.000,00

File_3
Code:
374905,2001, Selmar Santos, Técnico de Sistemas, U$1.000,00
4528791, 2008, Ana Paula Pertel, Supervisor, U$2.000,00

Result:

File_1
Code:
452879, 2008, Ana Paula Pertel, Supervisor, U$2.000,00
953585, 2010, Carolina Sant Ana, Analista Plena, U$10.000,00
582364, 2008, Célia Cupertino, Gerente, U$11.000,00
789502, 2008, Eduardo Calmon, Analista Sênior, U$8.000,00
879525,2001, Frederico Marlon, Técnico de Sistemas, U$3.500,00
798965, 2007, Jamile Cupertino, Analista Junior, U$6.000,00
374905,2001, Selmar Santos, Técnico de Sistemas, U$3.000,00

Please, help me in this question.

Thanks!

Last edited by Franklin52; 07-14-2010 at 10:16 AM.. Reason: Please use code tags
# 2  
Old 07-14-2010
Hi Selmar,

If I have understood u clear, this is what you require.
Data common to file_1 and file_3 should be deleted from file_1. Then file_2 and file_3 should be added to file_1.

Code:
comm -23 file_1 file_3 > temp.dat
mv temp.dat file_1
cat file_2 >> file_1
cat file_3 >> file_1

# 3  
Old 07-14-2010
the data are deleted from file_3, but the code helped me a lot, thanks.

Last edited by Scott; 07-14-2010 at 10:57 AM..
# 4  
Old 07-15-2010
Code:
 awk -F , 'NR==FNR{a[$1]=$0;next} !($1 in a) {print}
          END {for (i in a) print a[i]}' file_1 file_2 file_3


Last edited by rdcwayx; 07-15-2010 at 08:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files and lines which are the same just delete it

I am having a two files and different days, and this is example: file1: 06.09.2017. abcd 123 file2: 07.09.2017. abcd 1234 So what I want is that file2 with today's date contains only 1234, so where is a problem you would ask? Problem is here that I put these commands into routers,. and... (3 Replies)
Discussion started by: tomislav91
3 Replies

2. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

3. Shell Programming and Scripting

Compare data in two files

Gents, Can you help please I have a data base with lot information (file2) and I have some data in (file1) to compare. Then the comparison should be done using the following keys: Example ( values from file1 ) key1 = columns from 20-34 substr($0,20,15) 66705.00 19793 key2 = columns... (5 Replies)
Discussion started by: jiam912
5 Replies

4. Shell Programming and Scripting

Need shell script to compare directories and delete files on target server

Hello, I need help in writing the shell script for below mentioned case. There are 2 servers(server A, server B). A cronjob syncs files between these 2 servers. Existing script is copying files from A to B. This is done using the command rsync. However, the files are not deleted... (2 Replies)
Discussion started by: SravaniVedam11
2 Replies

5. Shell Programming and Scripting

Compare columns of two files and retrieve data

Hi guys, I need your help. I have two files: file1 1 3 5 file2 1,XX 2,AA 3,BB 4,CC 5,DD I would like to compare the first column and where they are equal to write that output in a new file: 1,XX 3,BB (7 Replies)
Discussion started by: apenkov
7 Replies

6. Shell Programming and Scripting

Compare two files and get matching data

Hi, Can anyone help me to compare two files and get the matching data... say i have file1 and file2 ... file1 has 300 unique data with that i need to match with file2 to see how may are matching.. file2 have 1000 records. (4 Replies)
Discussion started by: zooby
4 Replies

7. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

8. Shell Programming and Scripting

How to compare two flat files and get changed data

Hi, I need to compare two flat files (yesterday & today's data) and get only the changed data from flat files. In flat file i dont have data column or anything its just a string data in flat file.Can any one please let me know the script With Regds Shashi (3 Replies)
Discussion started by: jtshashidhar
3 Replies

9. Shell Programming and Scripting

Compare data in 2 files and delete if file exist

Hi there, I have written a script called "compare" (see below) to make comparison between 2 files namely test_put.log and Output_A0.log #!/bin/ksh while read file do found="no" while read line do echo $line | grep $file > /dev/null if then echo $file found found="yes" break fi... (3 Replies)
Discussion started by: lweegp
3 Replies

10. Shell Programming and Scripting

Compare data files

I would like to compare a data file (before and after a process has run) to identify if there are any differences. Can anyone help !! (1 Reply)
Discussion started by: ithomp
1 Replies
Login or Register to Ask a Question