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!

A simple grep command doesn't show a single line but every line containing the pattern. eg:

$ 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.

grep -c prints the count of matching lines per file. So, if

grep something file

displays a single line, then

grep -c something file

will print 1.

tyler_durden

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 ----------

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

Your grep looks broken. What OS and grep version are you using ?

$ 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 :frowning: 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!

Sorry for being direct but it would help if you:

  • answer to the questions asked
  • provide facts and reproducible tests instead of confusing statements.

Why does the length of the line matter ? Despite the length of the line, grep should display all lines that match the pattern.

tyler_durden

I am very new to unix shell... I'm trying to teach myself this stuff so that it will be useful in my future in my computer life :frowning: It is starting to get very frustrating so far. I mainly just have a grasp on sed and thats basically it.

Just a hunch, but check if there are any funky characters in your data file.

od -bc your_file

tyler_durden

funky characters meaning ] $ #? because if so then YES there are :stuck_out_tongue:

however I used sed to get rid of some of them, but not all of them since that is not what i'm worried about at this moment!
... or can it have an effect on the grep?

Paste the output of the "od -bc" command over here.

tyler_durden

ill post a sample :stuck_out_tongue: because its still going... 5mb file :slight_smile:

---------- Post updated at 01:36 AM ---------- Previous update was at 01:28 AM ----------

Do I have to sed out all of the #*&^%$ characters in my text that are scattered in the file in order for grep to work correctly?

Your data file is messed up. Run the following command and then run grep.

dos2unix your_file your_file

If you do not have "dos2unix" in your system, try "dos2ux".
Otherwise, you could try this -

perl -pi.bak -e 's/\r//g' your_file

Back up your file before you run any of the commands above.

tyler_durden

YOU FIXED MY PROBLEM!!! THANK YOU VERY MUCH!!! I thought I was just using grep wrong but it was the file all along!!!! I think it has to do with me copying and pasting text from an html document into nano through PUTTY!

THANK YOU.

$ dos2unix file
$ tr -d '\r' < file 
$ ruby -i.bak -pne 'gsub("\r","")' file