Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash.

I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i attempt to combine this with grep it seems to do nothing at all. Below is the command i am entering:

cat /etc/passwd | wc | grep bash

If anyone can help me correct this and indicate where i am going wrong i'd be most grateful.

try this:

cat /etc/passwd | grep "bash" | wc

or

cat /etc/passwd | grep "bash" | wc -l

if you just want the count.

Thanks for that but i have one more question if within /etc/passwd i wanted to write a command to replace each instance of bin/bash from every line with bin/csh without manually editing the file how would i do that?

use the stream editor, sed.

sed -i 's/bin\/bash/bin\/csh/g' /etc/passwd

the back slashes are escape characters for the front slashes. The g reperesents 'global,' and will change every instance, not just the first occurance on each line.

Hopefully this works.

$ 
$ cat f1
The first line.
This line has /bin/bash in it.
The third line.
This line has /bin/bash in it, twice => /bin/bash.
The fifth line.
And /bin/bash here and here /bin/bash and here /bin/bash
$ 
$ perl -pi.backup -e 's#/bin/bash#/bin/csh#g' f1
$ 
$ cat f1
The first line.
This line has /bin/csh in it.
The third line.
This line has /bin/csh in it, twice => /bin/csh.
The fifth line.
And /bin/csh here and here /bin/csh and here /bin/csh
$ 
$ cat f1.backup
The first line.
This line has /bin/bash in it.
The third line.
This line has /bin/bash in it, twice => /bin/bash.
The fifth line.
And /bin/bash here and here /bin/bash and here /bin/bash
$ 
$ 

tyler_durden

Warlock im not good at shell but let me help you as far as i can. Use SED for this and the command should be something like below:

sed 's/\/bin\/bash/\/bin\/csh/g' /etc/passwd

regards

awk -F\/ '$NF=="bash"{a++}END{print a}' /etc/passwd
egrep "bash$" /etc/passwd | wc -l

The POSIX standard does not have egrep; the equivalent is grep -E, but it is not necessary for this.

grep "bash$" /etc/passwd | wc -l

---------- Post updated at 03:37 AM ---------- Previous update was at 03:30 AM ----------

Why are you using cat?

Any why are you using wc instead of grep -c?