How to find lines that match exact input and count?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find lines that match exact input and count?
# 1  
Old 07-11-2010
Question How to find lines that match exact input and count?

I am writing a package manager in BASH and I would like a small snippet of code that finds lines that match exact input and count them. For example, my file contains:
Code:
xyz
xyz-lib2.0+
xyz-lib2.0
xyz-lib1.5

and "grep -c xyz" returns 4.

The current function is:
Code:
# $1 is the package name.
function check_pkg_install {
	foo=$(grep -c $1 /etc/spm/packages.list)
	if [ $foo -eq 1 ] ; then
		pkg_installed=1
		return 1
	else
		pkg_installed=0
		return 0
	fi
}

This is a summer project while I have no school, so you can take your time (but it shouldn't take 2 months...) Project is attached.
# 2  
Old 07-11-2010
You can make a array of packages matched.

Code:
i=0
for pkg in `grep $1 packages.list`
do
     packages[$i]="$pkg"
     i=$(expr $i + 1)
done

Now, the array $packages contains all grep results. Returns the count of matches:

Code:
> echo ${#packages[@]}
4

And you can check_version of every items of the array.

I hope this help. Smilie
# 3  
Old 07-11-2010
Try:
Code:
foo=$(grep -Fxc "$1" /etc/spm/packages.list)

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 07-12-2010
@Scrutinizer: Thank you. It worked when I copied and pasted it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Vlookup using awk without exact match for two colum input

Source file 1 335 R1-snapfound 0098F RDFType:R1 R2-Dev R2-snapfound ,010C0 RemoteSymmetrixID:345 335 R1-snapfound 00990 RDFType:R1 R2-Dev R2-snapfound ,010C1 RemoteSymmetrixID:345 335 R1-snapfound 009C0 RDFType:R1 R2-Dev R2-snapfound ,009C1 RemoteSymmetrixID:345 335 R1-snapfound 009C1... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. UNIX for Beginners Questions & Answers

Match file and find count

Hi All, I have transaction in one file.I want to match that to another file and find the number of time the transaction is available on the other file.I need to take each record from TRANSFILE and match that with SPEND FILE and find the number of counts of the transaction TRANSFILE: ... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

3. UNIX for Beginners Questions & Answers

Find command with Metacharacter (*) Should match exact filename

Hi, Below is list of files in my directory. -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:58 12345_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12346_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12347_kms_report.csv -rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59... (2 Replies)
Discussion started by: Balraj
2 Replies

4. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

5. Shell Programming and Scripting

Find the exact and best match between 2 files

Dear Forum, File1: Reference 4474189 United Kingdom Mobile 4474188 United Kingdom Mobile 4474187 United Kingdom Mobile 447 United Kingdom 93 AFGHANISTAN 0093 1907 ALASKA 001907 355 ALBANIA 00355 35568 ALBANIA MOBILE 0035568 35569 ALBANIA MOBILE 0035569 213 ALGERIA 00213 2137 ALGERIA... (2 Replies)
Discussion started by: yahyaaa
2 Replies

6. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

7. Shell Programming and Scripting

Help me with s script to find exact match

Hi, im extracting data from oracle DB. As the data is case sensitive, i have to extract the data which doesn't match exactly. im poor in unix scripting, can someone plz help me with a script. Here are the details. Need to compare the second column of the each line of the file1.csv with the data in... (5 Replies)
Discussion started by: JSKOBS
5 Replies

8. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

9. 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

10. UNIX for Dummies Questions & Answers

using sed or grep to find exact match of text

Hi, Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only) 1 Hello world fantasy. 2 Hello worldfuntastic. 3 Hello world wonderful. I would like to get all those lines of text that... (5 Replies)
Discussion started by: risk_sly
5 Replies
Login or Register to Ask a Question