Help with simple grep command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with simple grep command
# 1  
Old 10-01-2010
Help with simple grep command

I am currently having a problem with displaying multiple occurences of a line using grep/sed combination. Let's say that I do grep "$anything" $file.
When I do this it only displays a single line, but if I do grep -c "$anything" $file it says that there are 3 occurences of $anything, but how can I display those 3 occurences?
.... I have tried other ways such as awk /\$anything/ ....etc but I am currently stuck! please help!
# 2  
Old 10-01-2010
A simple grep command doesn't show a single line but every line containing the pattern. eg:
Code:
$ grep ftp /etc/services
ftp-data	20/tcp
ftp		21/tcp
tftp		69/udp
$ grep -c ftp /etc/services
3

Please provide more details about what you are experiencing.
# 3  
Old 10-01-2010
Quote:
Originally Posted by puttster
...Let's say that I do grep "$anything" $file.
When I do this it only displays a single line, but if I do grep -c "$anything" $file it says that there are 3 occurences of $anything, ...
grep -c prints the count of matching lines per file. So, if

Code:
grep something file

displays a single line, then

Code:
grep -c something file

will print 1.

tyler_durden
# 4  
Old 10-01-2010
say $a contains 5 rows of random digits, if I do grep "therandomdigits" $a... It only shows 1 line of digits, but If I do grep -c "therandomdigits" $a it shows 5 as output meaning 5 lines of output correct?

---------- Post updated at 09:23 PM ---------- Previous update was at 09:22 PM ----------

Quote:
Originally Posted by durden_tyler
grep -c prints the count of matching lines per file. So, if

Code:
grep something file

displays a single line, then

Code:
grep -c something file

will print 1.

tyler_durden
If $anything is a variable that is initialized as # shouldnt grep display all lines that contain #?
For my code, it only displays one line but counts 6 or so.

---------- Post updated at 09:29 PM ---------- Previous update was at 09:23 PM ----------

Better example

s=#
$a contains

stanle#y hey
tc#p ss#h
udp tcp
PDB CDB

grep -c "$s" $a =

2

but grep "$s" $a =

stanle#y hey
# 5  
Old 10-01-2010
Your grep looks broken. What OS and grep version are you using ?

Code:
$ echo $s
#
$ cat $a
stanle#y hey
tc#p ss#h
udp tcp
PDB CDB
$ grep "$s" $a
stanle#y hey
tc#p ss#h
$ grep -c "$s" $a
2

This User Gave Thanks to jlliagre For This Post:
# 6  
Old 10-01-2010
Quote:
Originally Posted by jlliagre
Your grep looks broken. What OS and grep version are you using ?

Code:
$ echo $s
#
$ cat $a
stanle#y hey
tc#p ss#h
udp tcp
PDB CDB
$ grep "$s" $a
stanle#y hey
tc#p ss#h
$ grep -c "$s" $a
2

I think its more under the lines of myself not understanding now to actually do this with my amount of text I have to display Smilie I'll post again if a new issue pops up... thanks for the help so far

---------- Post updated at 09:48 PM ---------- Previous update was at 09:44 PM ----------

I think my problem is happening because my line is so long that its only showing the first one, is there a way to change the output into seperate even fields? Meh, I don't even think thats the real problem either, I can get it to work out of my program but not inside it. It never displays the 3 lines of data that it says that it has with -c.


--- It seems to me that within my code, It only prints the last line of the file that contains what ever I am grepping, any idea? it counts a certain number but displays the LAST line that it counts!

Last edited by puttster; 10-02-2010 at 12:07 AM..
# 7  
Old 10-02-2010
Sorry for being direct but it would help if you:
- answer to the questions asked
- provide facts and reproducible tests instead of confusing statements.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Simple grep question

I hope someone can help me. I have a folder e.g. /opt/application Under that are many sub folders e.g. Folder1 Folder2 Folder3 Folder4 Folder5 Folder6 etc In some of these fodlers (not all of them) is a file called errors.log I need to run a grep that will start at... (3 Replies)
Discussion started by: gunnahafta
3 Replies

2. Shell Programming and Scripting

Help with simple RegEx on grep

Hello, I am trying to grep my log files for ORA errors, except ORA-00001. I have tried: grep 'ORA*!(-00001)' *.log but it is not working. Any help will be much appreciated. Thank you. (5 Replies)
Discussion started by: drbiloukos
5 Replies

3. Shell Programming and Scripting

Simple grep script

I'm trying to write a simple script to identify every user who tried to “sudo” on the system. I have the first portion down to grep the log file grep “sudo” /var/log/secure. What I want to do is have the script identify the person just one time not every instance the user tried... (4 Replies)
Discussion started by: bouncer
4 Replies

4. UNIX for Dummies Questions & Answers

Simple grep question

This should be so easy... I want to find all the apps in /Applications that start with the lower case i (e.g. iTunes.app, iSync.app, iCal.app) They should all have the .app extension. I've tried: ls /Applications |grep -o i*.app ls /Applications/i*.app Anyhow, I just want to see what apps... (2 Replies)
Discussion started by: glev2005
2 Replies

5. Shell Programming and Scripting

simple grep is not working for me

Hi, On the log Netscape log, I need to grep for 500 error. I am doing that but I also get 1500 in that same log. cat access |grep "500" Results: "GET /css/RBR.css HTTP/1.1" 200 15000 304 - - - 399 639 523 164 0 This not what I need... Please advice. (4 Replies)
Discussion started by: samnyc
4 Replies

6. Shell Programming and Scripting

Simple grep Question

I tried searching for answers but didn't find any. When I grep a file results read 4.2.2.2 4.4.4.2 4.5.6.7 But I just want to select each result individually. For Example I want to be able to say variable1="first grep result" variable2="second grep result" variable3="third grep... (8 Replies)
Discussion started by: elbombillo
8 Replies

7. UNIX for Dummies Questions & Answers

Simple newbie grep question

How come grep testfile1 won't find anything in testfile1 (even though the characters sd are there in great quantity), but grep '' testfile1 will find plenty? Do the single quotes prevent the shell from interpreting the testfile1 is interpreted as: grep *test whether or not characters sd exist*... (5 Replies)
Discussion started by: doubleminus
5 Replies

8. UNIX for Dummies Questions & Answers

Simple grep - Not sure it makes sense!

I have 3 files in directory mydir named as follows, I run the sequence of commands shown below and I have questions at the result. File names are: ABC_GP0 ABC_GP0.ctl ABC_GPX Commands and results: $ ls /mydir/ | grep * <-- (q1) I get nothing - OK $ ls /mydir/ | grep... (5 Replies)
Discussion started by: GNMIKE
5 Replies

9. UNIX for Dummies Questions & Answers

simple grep question

I have seen this used several times but not really sure of what it actually does. I am confused with the second grep as the argument to the first. some commands | grep -v grep | some other commands Can anyone provide an explanation? Thanks, (5 Replies)
Discussion started by: google
5 Replies

10. UNIX for Dummies Questions & Answers

Simple grep questions

Hi all, My boss wants me to find out how often e-m users are accessing their account:confused:. The mail server keeps log of all logins. I want to use grep the 'usernames', but it should come out the moment it first encounters the username in the log. Can I do that? I want to avoid 10+ greps... (2 Replies)
Discussion started by: nitin
2 Replies
Login or Register to Ask a Question