Use GREP to count number of records and place it in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use GREP to count number of records and place it in a variable
# 1  
Old 12-19-2013
Use GREP to count number of records and place it in a variable

I am trying to count the number of records from different files using grep, and then place the result in a separate variable for each file, so at the end of my shell script, I can sum all the variables and check if the number of records are equal to what I was expecting. It is weird butwc -ldoes not give me the correct number of records for the files. So for this scenario I have to use only grep command. Please see below how I was trying to use the command:
Code:
Total_records_file1=`grep -c '.*' file1.txt`
Total_records_file2=`grep -c '.*' file2.txt`
.
.
.
Total_records_file(n)=`grep -c '.*' file(n).txt`

Where (n)=Nth number of file

I have also tried escaping the single quotes like below and it doesn't work
Code:
Total_records_file1=`grep -c \'.*\' file1.txt`
Total_records_file2=`grep -c \'.*\' file2.txt`
.
.
.
Total_records_file(n)=`grep -c \'.*\' file(n).txt`

and then sum up the variables using the code below:
Code:
Total_Sum_of_Records_in_Files=`expr ${Total_records_file1:-0} + ${Total_records_file2:-0} + ...... + ${Total_records_file(n):-0}`

Please advise if there are any other suggestions...

Last edited by dhruuv369; 12-19-2013 at 05:38 PM.. Reason: Did not display any body for my question in the first time. This is happening every time I post a thread.
# 2  
Old 12-19-2013
try

Code:
var=$(awk 'END{print NR}' file1 file2 file3)

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 12-19-2013
Not sure why wc -l should NOT give you the correct No. of lines unless some of your files are not text files (e.g. <newline> missing at the end of file). Wouldn't cat file*.txt|wc -l give you the right line count?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign number of records to a variable

How does one assign a variable, x to equal the number of records in a different file. I have a simple command such as below: awk -F "\t" '(NR>5) { if(($x == "0/0")) { print $0} }' a.txt > a1.txt but I want x to equal the number of records in a different file, b.txt (10 Replies)
Discussion started by: Geneanalyst
10 Replies

2. UNIX for Dummies Questions & Answers

I want count of number of records to be printed on each row.

we want the count of number of records to be printed on each row. For Ex: if there are 5 records on one unique id , the count "5'' should be printed on each record in other column. Please help for this. I am using unix & Cygwin. Below are sample records: KCZ0650473... (2 Replies)
Discussion started by: ElijaRajesh
2 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. Shell Programming and Scripting

Count number of files and use result as variable

Hi there I have a .ksh script that I am using on an AIX ( Actual Level 5.3.10.0, Maintenance Level 5.3.0.0) where I am logging into a windows box, doing a file count on that server and returning the output to the UNIX session. I would like to exit the script at this point in time if the... (10 Replies)
Discussion started by: jimbojames
10 Replies

5. UNIX for Dummies Questions & Answers

Grep specific records from a file of records that are separated by an empty line

Hi everyone. I am a newbie to Linux stuff. I have this kind of problem which couldn't solve alone. I have a text file with records separated by empty lines like this: ID: 20 Name: X Age: 19 ID: 21 Name: Z ID: 22 Email: xxx@yahoo.com Name: Y Age: 19 I want to grep records that... (4 Replies)
Discussion started by: Atrisa
4 Replies

6. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

7. UNIX for Dummies Questions & Answers

number of occurence using grep -c then assigning it to a variable

Hi guys! I need to count the occurence of a certain pattern. For example the pattern is PC. the contents of the file sample.txt: A PC asdfgadfjkl asdfa PC sadfaf fdsPCasdfg if i use grep -c PC sample.txt it will display 3 as the number of occurence how do i save that number to a... (1 Reply)
Discussion started by: khestoi
1 Replies

8. UNIX for Dummies Questions & Answers

putting grep -c results number in a variable

I want to display "no results found" if a grep search of a name that the user inputs is not found anywhere in a certain file, Right now I have this, but doesn't seem to work. Im not sure what to change. read name results=grep -c $name file if ; then echo "No results found." exit... (1 Reply)
Discussion started by: busdude
1 Replies

9. Shell Programming and Scripting

grep all records in a file and get a word count -perl

Hi, I have a file .. file.txt .. i need to get a total record count in the files into a $variable.. im using perl script thanks (4 Replies)
Discussion started by: meghana
4 Replies

10. Shell Programming and Scripting

Count No of Records in File without counting Header and Trailer Records

I have a flat file and need to count no of records in the file less the header and the trailer record. I would appreciate any and all asistance Thanks Hadi Lalani (2 Replies)
Discussion started by: guiguy
2 Replies
Login or Register to Ask a Question