Comparing Two Files, Possibly with VIM


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing Two Files, Possibly with VIM
# 1  
Old 06-06-2012
Comparing Two Files, Possibly with VIM

Hello, I have a problem comparing two files, both .csv. One file, let's call it files.csv, contains a list of thousands of file names for models of molecules that have made it through an optimization process. The other file, formulas.csv, contains file names for the same kind of models, and has another column for the chemical formula of these molecules. This is valuable information that I would like to have to put in Excel spreadsheets, amongst other things. The problem is, formulas.csv contains all of the files in file.csv, but also many additional molecules that I have no need for. All the molecules I need are in files.csv. Thus, my question is, can I possibly compare the two files, and then delete any molecules from formulas.csv that are not contained in files.csv? If possible, I would like to do this in VIM, because I am fairly comfortable in that program, but I am not sure it is possible. So, any shell or Perl scripts that would do the job would also be greatly appreciated.

My explanation may have been vague and long winded, so here's an example:
files.csv
C5029663
C50313718
C503208
C50337014
C50338802
........

formulas.csv
C5029663, C4H7IO2
C50313718, C15H32O
C503208, C2H2FN
C503275, C2H4N2

C50337014, C9H18O
C503388, C2Cl4O2
C50338802, C6H10
........

The bolded lines in formulas.csv have no corollary in files.csv, and I would like to remove them. Any help would be appreciated. Thanks!
# 2  
Old 06-06-2012
Try:
Code:
awk -F, 'NR==FNR{a[$1]=1;next}$1 in a' files.csv formulas.csv > filtered.csv

PS. Please use code tags for sample data: https://www.unix.com/how-post-unix-li...code-tags.html
# 3  
Old 06-06-2012
Thank you very much for you help Bartus, and I will certainly use code tags in the future.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. UNIX for Dummies Questions & Answers

Error when using vim to open files

Hi all, 1st post here. I'm on Solaris and currently, I get the following error if I try to use vim to edit anything: Error detected while processing function <SNR>6_initialize: line 2: E697: Missing end of List ']': E116: Invalid arguments for function <SNR>6_defineOption line 3: E10:... (6 Replies)
Discussion started by: fred2028
6 Replies

3. UNIX for Dummies Questions & Answers

$PATH error (possibly)

Upon opening Terminal I get the following message: -bash: /usr/bin/manpath: No such file or directory -bash: /usr/bin/perl: No such file or directory -bash: grep: command not found -bash: grep: command not found -bash: grep: command not found -bash: grep: command not found I searched... (9 Replies)
Discussion started by: SartreSmartre
9 Replies

4. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

5. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 Replies

6. UNIX for Dummies Questions & Answers

Using Vim or Ex to rename all files in a folder

Is there a way to use Vim or Ex to rename all files in a folder? For example if i have a folder with Apples1.txt, Apples2.txt, Apples3.txt, could I rename all files so they are Oranges1.txt, Oranges2.txt, Oranges3.txt ? I know with :bufdo %s/apples/oranges/g|update you can do this for inside... (5 Replies)
Discussion started by: VimNewUser
5 Replies

7. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

8. UNIX for Dummies Questions & Answers

Syntax coloring for .pc files in VIM editor

Hi, i am using VIM editor through Putty. By the option of Syntax on in .vimrc file i am able to see syntax colors in .c and .cpp files but not in the files with .pc extension. How can this be done? :confused: (2 Replies)
Discussion started by: RuchK
2 Replies

9. Shell Programming and Scripting

stupid question possibly

how would I search through subdirectories under the current directory and delete all files in certain directories. in ThisDirectory.... want to go into foundMe directory which there are several in other subdirectories and delete all files in foundMe ? thank you... (1 Reply)
Discussion started by: MrJaunty
1 Replies
Login or Register to Ask a Question