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:
Where (n)=Nth number of file
I have also tried escaping the single quotes like below and it doesn't work
and then sum up the variables using the code below:
Please advise if there are any other suggestions...
Last edited by dhruuv369; 12-19-2013 at 06:38 PM..
Reason: Did not display any body for my question in the first time. This is happening every time I post a thread.
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?
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)
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)
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)
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)
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)
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)
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)
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)