[bash]regexp to test PrintableString


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash]regexp to test PrintableString
# 1  
Old 04-15-2009
[bash]regexp to test PrintableString

Hello all,

I would like some help to find the regexp to test that a word does not contains anything than the character set of printable strings.
Especially the word should not contain a underscore.

Code:
echo "$word" | grep "([A-Za-z][0-9] \'\(\)+,-\.:=\?)+"
if [ $? -eq 0 ]
then
  echo success
else
  echo failed
fi

Thanx for the help.
# 2  
Old 04-15-2009
Put all allowed characters into one character class for this to work, otherwise you're looking for a sequence of a letter, a number, space,... that occurs one or more time.
Code:
grep -qE "^[-A-Za-z0-9 ')(+,\.:=?]+$"

# 3  
Old 04-15-2009
To exclude a character you can use the -v option, for the underscore this should suffice:

Code:
grep -v '_'

If you want to exclude more characters use a class like this:

Code:
grep -v '[_~!]'

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bash condition test at the end of string

I want to check (using bash condition test function) if string contains three spaces, ignoring last three spaces at the end of string. string_to_report='foo bar foo bar ' string_to_ignore='foo bar ' (8 Replies)
Discussion started by: useretail
8 Replies

2. Shell Programming and Scripting

Bash Regexp failing: unexpected token `('

Hey guys, I'm fairly new to bash scripting, so bear with me :) What I want to achieve is reading a file (.php), find the phrase 'WACHTWOORD' (password) in it, and collect the password and a part of the filename in a list. The filename is always 'settings_{name}.php' and the part I'm looking... (6 Replies)
Discussion started by: Eggie01
6 Replies

3. Shell Programming and Scripting

Operator test in bash

Hello, can you please help me because I am totally confused with a simple script: #!/bin/bash ] || ] && echo "Good Morning" ] || ] && echo "Good Night" For me, these two strings are indentical: false || false and there is no point to execute echo command. But the run result is... (5 Replies)
Discussion started by: AndreiM
5 Replies

4. Shell Programming and Scripting

if test in bash - can't see error

Hi, I have the following script under bash if ; then echo File $file_name Not Found else echo File $file_name Found fi I get the message even the file is found; there is only one file with that name in the directory I can't figure... (4 Replies)
Discussion started by: f_o_555
4 Replies

5. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

6. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

7. Shell Programming and Scripting

bash script to test network connection - please help

I want to test if my host can connect to any of the following 10 hosts (192.168.1.0 to 192.168.1.9) I didnt know which command i should use, so i chose ping. (i wonder if i should use tracepath or sth else) my code is the following: #!/bin/bash clear firstPart="192.168.1" maxNum="9" ... (2 Replies)
Discussion started by: shadow_boi
2 Replies

8. UNIX for Dummies Questions & Answers

print the line immediately after a regexp; but regexp is a sentence

Good Day, Im new to scripting especially awk and sed. I just would like to ask help from you guys about a sed command that prints the line immediately after a regexp, but not the line containing the regexp. sed -n '/regexp/{n;p;}' filename What if my regexp is 3 word or a sentence. Im... (3 Replies)
Discussion started by: ownins
3 Replies

9. Shell Programming and Scripting

test-and-set in bash

I'm not talking about the assembly instruction TAS, a better name could be check-and-set :) Anyway, is there a way to simplify the following if ; then VAR="something"; fi I have ~20 variables that should be test-and-set like this, and it really looks lame. (2 Replies)
Discussion started by: rayne
2 Replies

10. Shell Programming and Scripting

regex test in bash

Hi I want to do a regex test and branch based on the test result, but this doesn't seems to work :confused: if \) ]] then echo success else echo failed fi (1 Reply)
Discussion started by: subin_bala
1 Replies
Login or Register to Ask a Question