LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   pattern matching (http://www.linuxquestions.org/questions/linux-newbie-8/pattern-matching-560700/)

nadeemr 06-10-2007 04:40 PM

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

dawkcid 06-10-2007 05:04 PM

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'

nadeemr 06-10-2007 05:13 PM

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

nadeemr 06-10-2007 09:07 PM

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

nadeemr 06-12-2007 12:09 PM

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

Hewson 06-12-2007 01:15 PM

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

Hewson 06-12-2007 01:20 PM

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.

nadeemr 06-12-2007 05:00 PM

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

Hewson 06-13-2007 11:05 AM

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


All times are GMT -5. The time now is 08:31 AM.