LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 06-10-2007, 04:40 PM   #1
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Rep: Reputation: 0
pattern matching


- takes letters as an argument and list all usernames matching the pattern
Am new to bash scripts.
here is what i've written but its not working :


#!bin/bash

for i in $(cut -f 1,3 -d: /etc/passwd) ; do
array[${i#*:}]=${i%:*}
done

echo "User ID $1 is ${array[$1]}."
echo "There are currently ${#array[@]} user accounts on the system."


#We use cut to create a list from fields 1 and 3 in the /etc/passwd file. Field 1 is the account name and field 3 is the user ID for the account. The script loops through this list using the user ID as an index for each array element and assigns each account name to that element. The script then uses the supplied argument as an index into the array, prints out the value at that index, and prints the number of existing array values.

any ideas?

cut -f1 -d: /etc/passwd | grep '^a' - this helps, but i would like to grep all letters of the alphabet
e.g : ./pattern.sh c will return all usernames starting with letter c

Last edited by nadeemr; 06-10-2007 at 07:30 PM.
 
Old 06-10-2007, 05:04 PM   #2
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
What's the problem? Seems to work OK for me.

Quote:
- takes letters as an argument and list all usernames matching the pattern
What's this? This has nothing to do with the script you posted. If you want to do what you say here (list usernames that match a pattern) then try:

Code:
cut -f1 -d: /etc/passwd | grep pattern
e.g. list all usernames beginning with a

Code:
cut -f1 -d: /etc/passwd | grep '^a'
 
Old 06-10-2007, 05:13 PM   #3
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dawkcid
What's the problem? Seems to work OK for me.



What's this? This has nothing to do with the script you posted. If you want to do what you say here (list usernames that match a pattern) then try:

Code:
cut -f1 -d: /etc/passwd | grep pattern
e.g. list all usernames beginning with a

Code:
cut -f1 -d: /etc/passwd | grep '^a'

ok it seems to work...but how can i list from all letters of the alphabet (a-z) in a list?
e.g : if i type ./pattern.sh n - this will list all users starting with n

Last edited by nadeemr; 06-10-2007 at 05:14 PM.
 
Old 06-10-2007, 09:07 PM   #4
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
ok it seems to work...but how can i list from all letters of the alphabet (a-z) in a list?
e.g : if i type ./pattern.sh n - this will list all users starting with n
 
Old 06-12-2007, 12:09 PM   #5
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by nadeemr
ok it seems to work...but how can i list from all letters of the alphabet (a-z) in a list?
e.g : if i type ./pattern.sh n - this will list all users starting with n

can u guys please hint me on how to output users ranging from a - z
e.g when i run the script ./pattern.sh g - this will list all users starting with letter g
thanx
 
Old 06-12-2007, 01:15 PM   #6
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
Quote:
Originally Posted by nadeemr
#!bin/bash
haven't really delved into what your trying doing yet, but right off the top this should be:

Code:
#!/bin/bash
cheers
 
Old 06-12-2007, 01:20 PM   #7
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
instead of
cut -f1 -d: /etc/passwd | grep '^a'
use
cut -f1 -d: /etc/passwd | grep "^$1"

$1 is the first command line argument to your script



for your own enjoyment head on over to

http://tldp.org/LDP/abs/html/

its an excellent source for BASH scripting.

Last edited by Hewson; 06-12-2007 at 01:28 PM.
 
Old 06-12-2007, 05:00 PM   #8
nadeemr
LQ Newbie
 
Registered: May 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Hewson
instead of
cut -f1 -d: /etc/passwd | grep '^a'
use
cut -f1 -d: /etc/passwd | grep "^$1"

$1 is the first command line argument to your script



for your own enjoyment head on over to

http://tldp.org/LDP/abs/html/

its an excellent source for BASH scripting.

hello $1 list all the users but what i want is to know how to list specific users for example when i type ./pattern n - this will list all the users starting with n
 
Old 06-13-2007, 11:05 AM   #9
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
The script I gave you does exactly that:


file contents:
Code:
#!/bin/bash
# filename: grepTest
cut -f1 -d: /etc/passwd | grep "^$1"
file execution:
Code:
$ grepTest a
adm
avahi
apache
$
-s
 
  


Reply

Tags
link


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
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
list files NOT matching a pattern smart_sagittari Linux - Newbie 9 05-20-2005 05:32 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM
Doubt in Pattern matching and vi editor.. suriyamohan Linux - General 2 08-19-2003 05:03 AM


All times are GMT -5. The time now is 09:15 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration