Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Word count of lines ending with certain word

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-29-2009
warlock129 warlock129 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 26 April 2010, 7:22 AM EDT
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 08-29-2009
sigtor77 sigtor77 is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 30 August 2009, 3:39 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
try this:

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

or

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

if you just want the count.
Sponsored Links
    #3  
Old Unix and Linux 08-29-2009
warlock129 warlock129 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 26 April 2010, 7:22 AM EDT
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
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?
    #4  
Old Unix and Linux 08-29-2009
sigtor77 sigtor77 is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 30 August 2009, 3:39 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #5  
Old Unix and Linux 08-29-2009
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 26 August 2016, 2:56 AM EDT
Posts: 1,950
Thanks: 11
Thanked 300 Times in 271 Posts
Quote:
Originally Posted by warlock129 View Post
...
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?

Code:
$ 
$ 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
Sponsored Links
    #6  
Old Unix and Linux 08-29-2009
EAGL€ EAGL€ is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 22 April 2016, 2:28 AM EDT
Location: izmir
Posts: 331
Thanks: 33
Thanked 13 Times in 13 Posts
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:


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

regards
Sponsored Links
    #7  
Old Unix and Linux 08-29-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 122 Times in 115 Posts
Quote:
Originally Posted by warlock129 View Post
I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash.

Code:
awk -F\/ '$NF=="bash"{a++}END{print a}' /etc/passwd

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
word count wc chaandana Fedora 5 05-05-2009 10:47 AM
Word Count s_chowhan UNIX for Dummies Questions & Answers 10 04-30-2009 03:16 PM
specified word count bhaviknp Shell Programming and Scripting 5 02-27-2008 10:23 AM
sed find word ending with eltinator Shell Programming and Scripting 2 08-22-2007 06:26 PM
word count showing wrong number of lines tselvanin UNIX for Dummies Questions & Answers 3 01-06-2004 10:33 PM



All times are GMT -4. The time now is 04:46 AM.