# nawk empty regular expression error

**URL:** <https://community.unix.com/t/nawk-empty-regular-expression-error/179330>\
**Category:** Shell Programming and Scripting\
**Created:** [December 20, 2007, 4:54am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330 "2007-12-20T04:54:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rjkz](https://community.unix.com/letter_avatar/rjkz/32/5_5575768a8748004e209b776fc1b2916d.png) [@Rjkz](https://community.unix.com/u/Rjkz)\
**Post date:** [December 20, 2007, 4:54am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/1 "2007-12-20T04:54:00Z")

</div>

Hello, All.  
please help me with this problem.  
i need to use variable as regular expression.  
some thing like this:

BEGIN {  
RS="\n";  
ORS="\n";  
reg\_exp = ".+Dec.+";  
# i mean that regular expression is any symbol before "Dec" and any symbol after it. For example \<Worksheet ss:Name="December"\>  
}

$0 ~ reg\_exp {  
print $0  
next  
}  
and i get: error empty regular expression.  
if i try like this

$0 ~ /.+Dec.+/{  
print $0  
next  
}  
everything is all right.  
thanks in advance

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [December 20, 2007, 5:03am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/2 "2007-12-20T05:03:17Z")

</div>

you can try

```nohighlight
reg_exp = "Dec"

```

---

<div class="post-metadata">

**Author:** ![Rjkz](https://community.unix.com/letter_avatar/rjkz/32/5_5575768a8748004e209b776fc1b2916d.png) [@Rjkz](https://community.unix.com/u/Rjkz)\
**Post date:** [December 20, 2007, 5:20am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/3 "2007-12-20T05:20:50Z")

</div>

thank you for your reply.  
it works.

but actually a get a reg\_exp during running the script

like this

$0 ~ /.+\<Worksheet ss:Name.+/{  
reg\_exp = substr($0,22,3)  
# i can print reg\_exp. the value of reg\_exp is "Dec"  
print $0  
next  
}  
and then i use this reg\_exp.  
the string which hold "\<Worksheet ss:Name" is before string which i compare with reg\_exp

and i stell get error

---

<div class="post-metadata">

**Author:** ![Klashxx](https://community.unix.com/user_avatar/community.unix.com/klashxx/32/326_2.png) [@Klashxx](https://community.unix.com/u/Klashxx)\
**Post date:** [December 20, 2007, 5:27am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/4 "2007-12-20T05:27:24Z")

</div>

Maybe this helps you:

```nohighlight
> cat kk
<Worksheet ss:Name="December">
<Worksheet ss:Name="November">

```

```nohighlight
> regex="Dec"
> awk ' /.+'"${regex}"'.+/ ' kk
<Worksheet ss:Name="December">

```

```nohighlight
> regex="Nov"                  
> awk ' /.+'"${regex}"'.+/ ' kk
<Worksheet ss:Name="November">

```

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/5_5575768a8748004e209b776fc1b2916d.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [December 20, 2007, 5:33am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/5 "2007-12-20T05:33:34Z")

</div>

> [@rjkz](#):
>
> thank you for your reply.  
> it works.
> 
> but actually a get a reg\_exp during running the script
> 
> like this
> 
> $0 ~ /.+\<Worksheet ss:Name.+/{  
> reg\_exp = substr($0,22,3)  
> # i can print reg\_exp. the value of reg\_exp is "Dec"  
> print $0  
> next  
> }  
> and then i use this reg\_exp.  
> the string which hold "\<Worksheet ss:Name" is before string which i compare with reg\_exp
> 
> and i stell get error

since you have .+ both infront of the "\<Worksheet ss:Name" and behind it, it is also equivalent to /\<Worksheet ss:Name/, there is no need to use .+ . However, for completeness, you should show a sample of your data file, and describe how you want your output to be.

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/5_5575768a8748004e209b776fc1b2916d.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [December 20, 2007, 10:37am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/6 "2007-12-20T10:37:33Z")

</div>

> [@rjkz](#):
>
> Hello, All.  
> please help me with this problem.  
> i need to use variable as regular expression.  
> some thing like this:
> 
> BEGIN {  
> RS="\n";  
> ORS="\n";  
> reg\_exp = ".+Dec.+";  
> # i mean that regular expression is any symbol before "Dec" and any symbol after it. For example \<Worksheet ss:Name="December"\>  
> }
> 
> $0 ~ reg\_exp {  
> print $0  
> next  
> }  
> and i get: error empty regular expression.  
> if i try like this
> 
> $0 ~ /.+Dec.+/{  
> print $0  
> next  
> }  
> everything is all right.  
> thanks in advance

You don't need the BEGIN{} part as it is the default action taken by nawk.

```plaintext
nawk '{
   regexp=".+Dec.+"
   if ($0 ~ regexp)
      print $0
}' file

```

---

<div class="post-metadata">

**Author:** ![Rjkz](https://community.unix.com/letter_avatar/rjkz/32/5_5575768a8748004e209b776fc1b2916d.png) [@Rjkz](https://community.unix.com/u/Rjkz)\
**Post date:** [December 21, 2007, 10:07am UTC](https://community.unix.com/t/nawk-empty-regular-expression-error/179330/7 "2007-12-21T10:07:41Z")

</div>

Hello, All.  
i had problem with connection yesterday and could not unswer.  
Thanks a lot.  
i've already solved my problem.  
my problem was:

i was trying to use reg\_exp before i assign this variable.  
my script was:  
BEGIN {  
RS="\n";  
ORS="\n";  
reg\_exp="";  
}  
$0 ~ /.+\<Worksheet ss:Name.+/{  
reg\_exp = substr($0,22,3)  
print $0  
next  
}  
$0 ~ reg\_exp {  
print $0  
print "TEST"  
next  
}

and input file was about:

Record\_1  
Record\_2  
Record\_3  
.......  
Record\_n  
\<Worksheet ss:Name="December"\>  
Record\_n+2  
.....

Bye
