LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2009, 10:54 AM   #1
sudz28
LQ Newbie
 
Registered: Nov 2008
Location: Finksburg, Maryland
Posts: 7

Rep: Reputation: 1
grep question


Linux/Unix newbie here with a quick and hopefully easy grep question. I have a log file I wish to go through and give me a count of the number of times a particular string is encountered; but, I want to be able to print back to the screen what it is I'm giving a number for, since I'm searching for several things. I have a few lines in my script file that say:
tail -100000 nameof.log | fgrep word | wc -l
If I run this it gives me a correct count, but just the number. How do I get it to let me print the word I'm searching for ahead of the count?
Thanks!
 
Old 01-20-2009, 12:12 PM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Well, you could shove that number into a variable

Code:
word_count=$(tail -100000 nameof.log) | fgrep word | wc -l)
echo word ${word_count}
or, you could do it all in the echo

Code:
echo word $(tail -100000 nameof.log) | fgrep word | wc -l)
The $( ) construct in bash allows you to run a command within a command. Very handy. Have a look at the Bash Guide for Beginners or the Advanced Bash Scripting Guide depending on how comfortable you are with Bash.
 
Old 01-20-2009, 01:58 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You could use the tee function, like this for example:

$ tail -100000 nameof.log | grep word | tee /dev/stderr | wc -l

That puts the matched line on stderr followed by the line count. If, instead, you want the information in a file, say "word.matched", something like this should work:

$ word="regexp";tail -100000 nameof.log | grep "$(word)" | tee "$(word)".matched | wc -l >> "$(word)".matched
 
Old 01-20-2009, 02:23 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or use awk:
Code:
$ cat testfile
hello pluto
hello mickey
ciao pippo
hello donald
$ word=hello
$ awk -v pattern=$word '$0 ~ pattern{count++}END{print pattern,"matches",count,"times"}' testfile
hello matches 3 times
$
 
Old 01-21-2009, 07:13 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by colucix View Post
Or use awk:
Code:
$ cat testfile
hello pluto
hello mickey
ciao pippo
hello donald
$ word=hello
$ awk -v pattern=$word '$0 ~ pattern{count++}END{print pattern,"matches",count,"times"}' testfile
hello matches 3 times
$
Well, sure, although the OP did specify grep.

A slight change to the above program will also print each line containing a match:

awk -v pattern="${word}" '$0 ~ pattern{count++;print;}END{print pattern,"matches",count,"times"}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep question torrent478 Linux - Newbie 2 10-08-2008 06:50 PM
grep question tekmann33 Linux - Newbie 1 08-11-2008 11:22 AM
question about grep new_2_unix Linux - Newbie 5 12-07-2007 04:45 PM
grep question Hockeyfan Linux - General 1 05-15-2004 02:09 AM
grep question keyops Programming 2 04-09-2004 09:17 PM


All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration