Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Exclamation point in Bash

Shell Programming and Scripting


Tags
bash exclamation point

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-19-2009
Enobarbus37 Enobarbus37 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 25 July 2010, 10:25 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation point in Bash

Here is a script using egrep that extracts the last word in each line:

egrep -o " [a-zA-Z'-]+[,:.!';?]*$" File.txt > Results.txt

If it is placed in a file with the requisite header:

#!/bin/bash

it works perfectly.

If on the other hand, one attempts it from the command line, one gets the following error:

-bash: !': event not found

The problem is the exclamation point in the regexp.

I have tried numerous ways to get it to work from the command line and failed: using single quotes, escaping the !, etc.

Nothing works.

Now, you may ask, if it works from a file, why insist on doing it from the command line? You're right. I shouldn't. And my research indicates that this problem is particularly annoying in bash and that people simply recommend using a different shell.

But can it be solved?

Thanks in advance...
Sponsored Links
    #2  
Old Unix and Linux 05-19-2009
aigles's Unix or Linux Image
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 1 December 2014, 9:03 AM EST
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 62 Times in 58 Posts
Escape the !
On my pC whith Cygwin :

Code:
$ egrep -o " [a-zA-Z'-]+[,:.!';?]*$" file.txt > result.txt
bash: !': event not found

$ egrep -o " [a-zA-Z'-]+[,:.\!';?]*$" file.txt > result.txt
$

Jean-Pierre.
Sponsored Links
    #3  
Old Unix and Linux 05-19-2009
Enobarbus37 Enobarbus37 is offline
Registered User
 
Join Date: Feb 2008
Last Activity: 25 July 2010, 10:25 AM EDT
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I had only recently started using the double quotes in the expression, and I had not yet gotten around to escaping the ! in the double quotes.

So, you are right. Félicitations et chapeau. Mille mercis.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to make your bash script run on a machine with csh and bash npatwardhan Shell Programming and Scripting 3 11-19-2008 03:17 AM
BASH: floating point exception mapleleaves Shell Programming and Scripting 5 08-22-2008 08:25 AM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
Floating point numeric comparisions in bash borncrazy Shell Programming and Scripting 2 03-27-2005 06:39 PM
Fibre connection Point to Point SUN kie UNIX for Advanced & Expert Users 6 05-20-2003 11:20 AM



All times are GMT -4. The time now is 11:38 PM.