number of lines returned from a grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting number of lines returned from a grep command
# 1  
Old 10-09-2008
number of lines returned from a grep command

hi all,
from a shell (ksh) script, i am doing a 'grep'. how do i find out the number of lines returned from that 'grep' command ??

thanks in advance.
# 2  
Old 10-09-2008
use grep -c option
# 3  
Old 10-09-2008
thanks for the reply. grep -c only Prints only a count of the lines that contain the pattern. i wanted the actual line and also the number of lines returned as well.

is there a way to check the length of a string in unix ?? i could possibly use this to see how many lines were returned.
# 4  
Old 10-09-2008
Use AWK (nawk or /usr/xpg4/bin/awk on Solaris):

Code:
awk 'END{print c}/pattern/&&++c' input


Last edited by radoulov; 10-09-2008 at 08:09 PM.. Reason: Corrected: c++ to ++c :)
# 5  
Old 10-09-2008
line=$(grep -n "$pattern" "$filename" | cut -f1 -d':')
found=$(echo $line | awk '{print NF}'
a2156z
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command to show the number of results

Hi I wanted to know if there is an option in grep command to show the number of results (not the number of lines of findings). Thanks (14 Replies)
Discussion started by: abdossamad2003
14 Replies

2. UNIX for Beginners Questions & Answers

How to output non-number lines with grep?

I want to check my data quality. I want to output the lines with non-number. I used the grep command: grep '' myfile.csv Since my file is csv file, I don't want to output the lines with comma. And I also don't want to output "." or space. But I still get the lines like the following:... (8 Replies)
Discussion started by: twotwo
8 Replies

3. Shell Programming and Scripting

How to print N number of lines before and after the grep?

Hi , My record file , need to print up to above (DATA array)(there may be n no lines ) , grep "myvalue" row now .....suggest me some options --- DATA Array--- record type xxxxx sequence type yyyyy 2 3---> data1 /dev/ --- DEVICE --- MAXIMUM_People= data_blocks= MY_value=2 xyz abc ... (0 Replies)
Discussion started by: Huvan
0 Replies

4. Shell Programming and Scripting

Grep lines for number greater than given number

Hello, I am newbie to bash scripting. Could someone help me with the following. I have log file with output as shown below **************************LOG************************* 11/20/2013 9:11:23.64 Pinging xx.xx.xx.xx with 32 bytes of data: 11/20/2013 9:11:23.64 Reply from xx.xx.xx.xx:... (4 Replies)
Discussion started by: meena_2013
4 Replies

5. Shell Programming and Scripting

Grep range of lines to print a line number on match

Hi Guru's, I am trying to grep a range of line numbers (based on match) and then look for another match which starts with a special character '$' and print the line number. I have the below code but it is actually printing the line number counting starting from the first line of the range i am... (15 Replies)
Discussion started by: Kevin Tivoli
15 Replies

6. Shell Programming and Scripting

Extracting a certain number with grep command

Hello Friends, when I type grep '275' myfile.txt it gives me all the lines including 275 but it also gives others such as 0.8827588 or 1127507. I only want the lines including 275. How can I extract only 275 without other numbers including 275. thanks (2 Replies)
Discussion started by: rpf
2 Replies

7. Shell Programming and Scripting

downsizing of strings which are returned by grep

hey every1, i am a very new shell programmer. what i am trying to do is to rename a music file using its metadata(using mminfo) the problem is, mminfo's output is very weird: my final filename should be "$artist - $title" however, when i use artist=` mminfo ~/Desktop/It\'s\ Not\ My\... (16 Replies)
Discussion started by: abhigyan91
16 Replies

8. IP Networking

Port number of socket returned by accept()

Hi, I typed a few tcp/ip client/server examples from a book and it works - sort of - but I noticed something strange. When I run my server I set it to use port 3001 and the client uses the same port to connect to server. They succeed, but the server prints something that doesn't really make much... (0 Replies)
Discussion started by: idelovski
0 Replies

9. Shell Programming and Scripting

recording lines returned from fgrep...

Ok, I'm too used to c/java. I want to go through a file to search for lines matching a given string using: fgrep "matchString" anyfile.dat; This works like it should, but I would like to record the lines in an array, rather than echo them. Any ideas? :eek: Thanks. P.S. I'm using ksh. (3 Replies)
Discussion started by: DrRo183
3 Replies

10. UNIX for Dummies Questions & Answers

Copying file names returned from a grep command into another directory

When I do the following : grep -l "string" *, I get a list of file names returned. Is there a way to copy the files returned from the list into another directory ?. Thanks. (4 Replies)
Discussion started by: Kartheg
4 Replies
Login or Register to Ask a Question

Featured Tech Videos