LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Urgent (https://www.linuxquestions.org/questions/linux-newbie-8/urgent-596278/)

kkpal 11-01-2007 05:25 AM

Urgent
 
I am writing a "Registeration program" code using gtkdialog(in bash scripting) . I make a log file into which user's information are saved.
problem:
I have to read that file to check, username should not contain space or any kind of special character. i am taking too much time for it, plz help me

dansmith127 11-01-2007 07:25 AM

Use egrep
 
OK so let's say your usernames are saved in a file called users.txt

Do this:

egrep [^A-Za-z] users.txt

This will output all the 'bad' usernames - any line of the file where the username contains characters which are not upper or lower case letters.

Note this also excludes numbers. If you want to allow numbers, change the pattern to:

egrep [^A-Za-z0-9] users.txt

Google 'regex' to learn more about specifying patterns. A brief explanation of the pattern in my example:

in this context, the ^ symbol means we are matching any line that contains something other than the following patterns, which are alphabet uppercase (the A-Z part), then lowercase (a-z) then numbers (0-9).

If you want to capture the results in a file, just do

egrep [^A-Za-z] users.txt > bad_usernames.txt


All times are GMT -5. The time now is 10:04 AM.