Bash Vs. Bourne REGEX: metacharacters escaping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Vs. Bourne REGEX: metacharacters escaping
# 1  
Old 03-20-2013
Bash Vs. Bourne REGEX: metacharacters escaping

I am currently reading a very old reference from O'Reilly: Sed and Awk 2nd Edition reprinted in 2000. So far, it's a solid read and still very relevant. I'd highly recommend this to anyone.

The only problem I have with this book is that I had to resort to bourne shell to get my examples to work since bash wasn't ubiquitous when the book was written.

So, when I was trying to follow the book example, I get the error in bash on my latest Ubuntu Server distro.

I tried to use a regular expression that looked for any line containing the string, "book" in the bookworks file. It is not my ultimate goal to correctly extract all lines with the string "book" yet as I was following the book examples, which will show me the correct form.

I tried the following command in bash:
Code:
grep " [\"[{(]*book[]})\"?!.,;:'s]* " bookwords

I get the following error in bash:
Code:
-bash: !.,: event not found

But when I tried the command after switching over to bourne shell, I get no error, and it gave me the output I expected like the one in the book examples. Can someone please tell me why is this happening? I'd like to know what metacharacters are causing this and how I can escape it in Bash? I wish there is a third edition of this book that covers REGEX in bash.
# 2  
Old 03-20-2013
That is because of bash's history expansion. It tries to expand the exclamation mark. Try switching it off first:
Code:
set +H

Then it should work.

You could also escape the exclamation mark(s):
Code:
grep " [\"[{(]*book[]})\"?\!.,;:'s]* " bookwords

Or use single quotes but in your example they use double quotes in order to use a single quote in the expression. You could make a combination of a string that is part in single quotes, followed by a part in double quotes:
Code:
grep '["[{(]*book[]})"?!.,;:'"'s]* " bookwords


Last edited by Scrutinizer; 03-20-2013 at 12:55 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-20-2013
Thanks for the quick reply. I'm glad to be done with this mystery.
# 4  
Old 03-20-2013
That it's willing to expand history !'s inside quotes is one of the very few things that really enrages me about bash... A character that expands when quoted and doesn't when not, but only at certain times and not others, seems very hacky.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with bash escaping while using screen command

Hello, everyone. I'm currently trying to write a command system for a Minecraft server using screen. Here are the scripts I'm currently using. 0.sh #!/bin/bash screen -S Test114 -dm java -Xmx4G -jar server.jar nogui 1.sh #!/bin/bash args="$@" args2="${args@Q}" #args3=`printf '%q\n'... (2 Replies)
Discussion started by: Develon
2 Replies

2. UNIX for Dummies Questions & Answers

Regex for (a|b) in bash

I am trying to find files using the following by using simple bash script: if -2014 ]]; then echo "yes";fi What I need to find are any files with date 08-**-2014 so August 2014 any files. I can use if -2014 ]]; then echo "yes";fi That works fine. How do I get files beginning with 08... (1 Reply)
Discussion started by: newbie2010
1 Replies

3. Shell Programming and Scripting

Wanted: Help with escaping bash command line arguments

Please forgive me if this is the wrong forum. I want to execute some one liners with the groovy programming language and I'm having trouble escaping the special characters to accommodate bash. Here is one of the lines that is giving me trouble: groovy -e "(new... (1 Reply)
Discussion started by: siegfried
1 Replies

4. Shell Programming and Scripting

escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character. e.g /some/path/to/a/file.png /some/path to/another/file.jpeg /some path/to yet/another/file Notice that these paths may contain metacharacters, the spaces for example are also not escaped. If I wanted... (5 Replies)
Discussion started by: cue
5 Replies

5. Shell Programming and Scripting

Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do for (( j=1; j<=3; j++ )); do for (( k=1; k<=3; k++ )); do echo $i$j$k done done done Will the above code work on a BOURNE shell? As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using... (7 Replies)
Discussion started by: navienavnav
7 Replies

6. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

7. Shell Programming and Scripting

Bash, Bourne, and nullglob

I have a script that start out with this: #!/sbin/sh Several things run. However I cannot get: shopt -s nullglob to run in Bourne. I get: shopt: not found So within the main script (after #!/sbin/sh at the top) I start bash with: bash and try to run what I need with: shopt -s... (2 Replies)
Discussion started by: crowman
2 Replies

8. Shell Programming and Scripting

Bourne sh versus bash

Hi All, I know difference between shell(s) we are using, ie. sh, bash etc. But while writing shell script, is there any difference which shell I am using. and if yes, what are they? (4 Replies)
Discussion started by: Deei
4 Replies

9. Shell Programming and Scripting

Escaping '*' in Bash

I have the following situation ============ export DirectoryName=/tmp/xyz if ; then some_new_env=$DirectoryName"/*" ======================= I tried all the ways of escaping the '*', but still the shell seems to expand the '*' character. I want some_new_env to contain "/tmp/xyz/*" ... (7 Replies)
Discussion started by: rkshukla14
7 Replies

10. Shell Programming and Scripting

bash script help: escaping the '*'

hi there. i have a simple bash script that reads a word from a text file one at a time, and if a '*' character is encountered, it prints a message. however it doesn't work as i expected. :o what am i doing wrong here? thanks very much for the help :) for word in `cat $DOC` do if... (18 Replies)
Discussion started by: mark_nsx
18 Replies
Login or Register to Ask a Question