LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2012, 12:49 PM   #1
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Rep: Reputation: 17
bash script using regular expression


I need these characters ( ? \ . [ ] ^ $ ) removed from the
Code:
[:print:]
portion of this command.

Code:
< /dev/urandom tr -dc [:print:] | head -c 23
I have been searching this all day and have yet to find an answer. So thought I would let some of you look at it and possibly help me out.

Thanks.

Last edited by edwardcode; 05-30-2012 at 01:08 PM.
 
Old 05-30-2012, 01:44 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What is "this command"?
 
Old 05-30-2012, 02:23 PM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
I think (I could be wrong) that he's trying to generate pseudo-random strings, and that his quoted code may have at least one typo.

@edwardcode: You could include another tr(1) invocation in your pipeline to squeeze out the specific characters you don't want.
Code:
#!/bin/sh

PATH=/bin:/usr/bin

cat /dev/urandom | 
	tr -d '?\.[]^$' | 
	tr -dc '[:print:]' | 
	head -c 23

exit 0
Or you could do something like (with a more limited set of characters):
Code:
$ cat /dev/urandom | tr -dc '[:alnum:]' | head -c 23
NB: If what you're trying to do is generate pseudo-random passwords, then consider installing pwgen(1) instead.
 
1 members found this post helpful.
Old 05-30-2012, 04:42 PM   #4
edwardcode
Member
 
Registered: Apr 2010
Posts: 161

Original Poster
Rep: Reputation: 17
There is no typo because that line currently works. The purpose of this line is to randomly generate a password that is 23 char. long. This password has to have num, symbols, upper, and lower chars.. This is only one line of my script and I pass the output of this line in to the expect scripting language. The only problem is when I pass it the random password it does not like these chars. ? \ . [ ] ^ $ { }. I know that there is a way regular expressions can handle this, because I have seen similar code I am just having problems getting it to work in my case.
 
Old 05-31-2012, 02:05 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So is there an issue with anomie's solutions?
 
Old 05-31-2012, 02:07 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"[:print:]" is a character class. It contains a pre-defined list of common characters used in regex tests. Let's see what it's supposed to contain (as taken from the grep info page):

Code:
`[:print:]'
     Printable characters: `[:alnum:]', `[:punct:]', and space.
So lets also look up [:alnum:] and [:punct:]:

Code:
`[:alnum:]'
     Alphanumeric characters: `[:alpha:]' and `[:digit:]'; in the `C'
     locale and ASCII character encoding, this is the same as
     `[0-9A-Za-z]'.

`[:punct:]'
     Punctuation characters; in the `C' locale and ASCII character
     encoding, this is `! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \
     ] ^ _ ` { | } ~'.
(You can look up [:alpha:] and "[:digit:]" on your own if you need to, but they're pretty self-explanatory.)


There are two things clear from this. First, the results are dependent to some degree on your current locale, so be sure to specifically set your script to the C locale first if you want it to be consistent. And second, that these are fixed definitions. You can't exclude a character that's in a set. If none of the pre-defined sets work for you, you have to set up your own, customized list of characters in a regex bracket expression.

On the other hand, if you want to expand a list, you can also combine character classes, either with each other or with individual characters. To create a list of just the characters you want you need to do something like this:

Code:
$'[-\"\'!#%&()*+,/:;<=>@_`|~ [:alnum:]]'
Note also that there are some limitations as to the position certain characters can be in when included in a bracket expression (']' needs to be first, '^' can't be first, and '-' needs to be first or last), and you have to ensure that anything that can be interpreted by the shell is properly escaped as well. In this case the $'' quoting style (not posix, but supported by most by modern shells) can be very handy, as it expands certain backslash escapes into their literal equivalents. See the bash man page for details. (Actually, I'm not completely sure I built the above string correctly. It still needs to be tested.)

If you're using tr though, it can be a bit easier, as you don't need to build a full [] bracket expression, just a list of the raw characters.

Last edited by David the H.; 05-31-2012 at 02:15 AM. Reason: fixt a big oops!
 
  


Reply



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
Bash Script / Regular Expression Problem rm_-rf_windows Linux - General 4 03-28-2012 01:05 PM
[SOLVED] [bash] rm regular expression help RaptorX Programming 26 08-01-2009 06:29 PM
bash: checking if a variable is a number (need regular expression help) anonguy9 Linux - Newbie 6 03-29-2009 02:37 AM
What is meaning about the regular expression pertaining to vim script? haochao Programming 2 03-25-2009 12:08 AM
Using regular expression in expect script nik1984 Programming 1 08-28-2008 06:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:13 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration