The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find out command has returned any value? johnl Shell Programming and Scripting 1 07-28-2008 11:59 PM
returned from remote command Frank2004 HP-UX 2 04-30-2008 08:29 AM
recording lines returned from fgrep... DrRo183 Shell Programming and Scripting 3 04-07-2006 08:54 AM
Copying file names returned from a grep command into another directory Kartheg UNIX for Dummies Questions & Answers 4 11-17-2005 09:34 AM
w/who command returned zero users sinyem UNIX for Advanced & Expert Users 6 10-21-2003 08:06 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-09-2008
Registered User
 

Join Date: Sep 2007
Posts: 31
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.
Reply With Quote
Forum Sponsor
  #2  
Old 10-09-2008
vidyadhar85's Avatar
The Tutor
 

Join Date: Jun 2008
Location: INDIA
Posts: 537
use grep -c option
Reply With Quote
  #3  
Old 10-09-2008
Registered User
 

Join Date: Sep 2007
Posts: 31
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.
Reply With Quote
  #4  
Old 10-09-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,859
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 03:09 PM. Reason: Corrected: c++ to ++c :)
Reply With Quote
  #5  
Old 10-09-2008
Registered User
 

Join Date: Oct 2008
Posts: 14
line=$(grep -n "$pattern" "$filename" | cut -f1 -d':')
found=$(echo $line | awk '{print NF}'
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0