View previous topic :: View next topic |
Author |
Message |
helmutvandeshaft Tux's lil' helper
Joined: 23 Sep 2004 Posts: 90 Location: Amsterdam
|
Posted: Thu Jun 09, 2011 10:08 pm Post subject: help comparing files |
|
|
Hi, I hope somebody can help me with this.
I need to check two files, if something is in the first file I need to either remove the line from the second file or pipe it out to a new file.
file1 will look like
serial1
serial2
serial3
file2 will look like
serial1,cm1,mta1,key1
serail2,cm2,mta2,key2
serial3,cm3.mta3,key3
serial4,cm4,mta4,key4
serial4,cm5,mta5,key5
What I need is to check is file1, if the serial exists in file1 and file2 then i need to remove the whole line from file2.
file1 contains 259 serials and file2 contains 57000 lines so I should finish with 56741 lines in the output file or in file2.
I tried sorting the files and using comm but I guess I must have dome something wrong.
The output either ended up with more lines or no lines.
I hope this is clear and thanks for any help. |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Thu Jun 09, 2011 10:43 pm Post subject: |
|
|
slurpey.awk: | #!/usr/bin/awk
# An example for helmutvandeshaft from the Gentoo forums.
BEGIN {
# Set the field separator to comma.
FS = ",";
# Slurp up all the serial numbers from file1.
File1 = ARGV[1]; ARGV[1] = "";
while ((getline <File1) > 0) {
SernoList[$1] = 1;
}
}
# Select all records where the serial number *does not* match the list.
SernoList[$1] != 1 | Invoke it with Code: | awk -f slurpey.awk file1 file2 | This works well so long as file1 doesn't contain more than several thousand records and doesn't require either file to be sorted.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
helmutvandeshaft Tux's lil' helper
Joined: 23 Sep 2004 Posts: 90 Location: Amsterdam
|
Posted: Thu Jun 09, 2011 11:00 pm Post subject: help comparing files |
|
|
Perfect, this does exactly what I want.
Thanks for the reply |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|