Compare two files and count number of matching lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files and count number of matching lines
# 1  
Old 08-04-2014
Compare two files and count number of matching lines

Dear All,
I would like to compare two files and return the number of matches found.
Example

File A
Code:
Lx2
L1_Mus1
L1Md_T
Lx5
L1M2
L1_Mus3
Lx3_Mus
Lx9
Lx2A
L1Md_A
L1Md_F2

File B
Code:
L1_Mus3
L1_Mus3
L1_Mus3
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1
L1_Mus1


Desidered Output
File A
Code:
Lx2   0
L1_Mus1   13
L1Md_T    0
Lx5   0
L1M2   0
L1_Mus3   3
Lx9   0
Lx2A   0
L1Md_A   0
L1Md_F2   0

Could You please help me?

Many thanks,
Paolo
# 2  
Old 08-04-2014
try

Code:
awk 'NR==FNR{a[$0]++;next}{if ($0 in a){print $0,a[$0]} else {print $0,0}}' file B file A

This User Gave Thanks to protocomm For This Post:
# 3  
Old 08-04-2014
Code:
$ awk 'FNR==NR{ A[$1]++; next}{ print $1 , A[$1]+0 }' fileB fileA

This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 08-04-2014
Many many thanks,
Paolo
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

2. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

3. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

4. UNIX for Dummies Questions & Answers

Count Number Of lines in text files and append values to beginning of file

Hello, I have 50 text files in a directory called "AllFiles" I want to make a program that will go inside of the "AllFiles" Directory and count the number of lines in each individual text file. Then, the program will calculate how many more lines there are over 400 in each text file and... (7 Replies)
Discussion started by: motoxeryz125
7 Replies

5. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

6. UNIX for Dummies Questions & Answers

Comparing two files and count number of lines that match

Hello all, I always found help for my problems using the search option, but this time my request is too specific. I have two files that I want to compare. File1 is the index and File2 contains the data: File1: chr1 protein_coding exon 500 600 . + . gene_id "20532";... (0 Replies)
Discussion started by: DerSeb
0 Replies

7. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

8. Shell Programming and Scripting

count the number of lines that start with the number

I have a file with contents similar to this. abcd 1234 4567 7666 jdjdjd 89289 9382 92 jksdj 9823 298 I want to write a shell script which count the number of lines that start with the number (disregard the lines starting with alphabets) (1 Reply)
Discussion started by: grajp002
1 Replies

9. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

10. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies
Login or Register to Ask a Question