The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular expression maxvirrozeito UNIX for Dummies Questions & Answers 1 12-14-2007 08:02 AM
error: initializer expression list treated as compound expression arunchaudhary19 High Level Programming 12 11-16-2007 06:44 AM
regular expression...help!! andy2000 UNIX for Dummies Questions & Answers 6 07-18-2007 05:10 PM
regular expression help mac2118 Shell Programming and Scripting 5 03-01-2005 07:45 AM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-20-2007
Rjkz Rjkz is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 3
nawk empty regular expression error

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
  #2 (permalink)  
Old 12-20-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
you can try
Code:
reg_exp = "Dec"
  #3 (permalink)  
Old 12-20-2007
Rjkz Rjkz is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 3
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
  #4 (permalink)  
Old 12-20-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
Quote:
Originally Posted by Rjkz View Post
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.
  #5 (permalink)  
Old 12-20-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Maybe this helps you:

Code:
> cat kk
<Worksheet ss:Name="December">
<Worksheet ss:Name="November">
Code:
> regex="Dec"
> awk ' /.+'"${regex}"'.+/ ' kk
<Worksheet ss:Name="December">
Code:
> regex="Nov"                  
> awk ' /.+'"${regex}"'.+/ ' kk
<Worksheet ss:Name="November">
  #6 (permalink)  
Old 12-20-2007
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
Quote:
Originally Posted by Rjkz View Post
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.

Code:
nawk '{
   regexp=".+Dec.+"
   if ($0 ~ regexp)
      print $0
}' file
  #7 (permalink)  
Old 12-21-2007
Rjkz Rjkz is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 3
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:45 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0