awk variables in regex expression ?

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 01-07-2011
awk variables in regex expression ?

Hello,

Could someone explain why this one returns nothing:
Code:
$ x=/jon/
$ echo jon | awk -v xa=$x '$1~xa {print}'
$

while the following works fine:
Code:
$ x=jon
$ echo jon | awk -v xa=$x '$1==xa {print}'
$ jon

and the following works fine:
Code:
$ echo jon | awk '$1~/jon/ {print}'
$ jon

using bash.

thanks
V

Last edited by vilius; 01-07-2011 at 05:34 AM..
# 2  
Old 01-07-2011
Quote:
Originally Posted by vilius
Hello,

Could someone explain why this one returns nothing:
Code:
$ x=/jon/
$ echo jon | awk -v xa=$x '$1~xa {print}'
$

When a pattern matching is done using ~ we need to give the reg-exp in between / / (2 forward slashes).Still, when given inside the slashes it is taken as literally, so when changing the above awk as
Code:
echo jon | awk -v xa=$x '$1~/xa/ {print}'

it matches the word xa and not the variable xa's value.Hence we need to expand the variable as below
Code:
x=/jon/
echo jon | awk -v xa=$x '$1~/'"$xa"'/ {print}'

This is not the case when == is used as you would know this is used to equate between variables.Therefore in code $1==xa.. xa is expanded (to jon).
# 3  
Old 01-07-2011
thanks that helped very much!
# 4  
Old 04-29-2011
Quote:
Originally Posted by michaelrozar17
Code:
x=/jon/
echo jon | awk -v xa=$x '$1~/'"$xa"'/ {print}'

I have a question about something similar.

Code:
awk ' 
BEGIN { split(" ' " ` date ` " ' ", date) } 
$1 == date[2] && $2 == date[3] {print $0}
' $1

Why are there two pairs of quotes? I would say that there is one pair of double quotes too many, but when I remove them, it produces an error (different, depending on which pair I remove).

P.S. The script is meant to be a calendar reminder. There is an additional text file. It contains something like the following text:

Code:
Sep 30  mother's birthday
Oct  1  lunch with joe, noon
Oct  1  meeting 4pm

Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regex Expression Replace dr46014 UNIX for Beginners Questions & Answers 7 07-26-2017 05:08 AM
Sed: -e expression #1, char 16: unterminated address regex rbadveti Shell Programming and Scripting 23 10-02-2016 07:06 AM
awk regex expression works in AIX but not in Linux kamlesh_pradhan Shell Programming and Scripting 8 07-28-2014 01:25 PM
Get variables from expression zulfi123786 Shell Programming and Scripting 8 05-14-2014 12:02 PM
Hi im new to bash scripting I want to know what does the regex expression do ?? kevin298 Shell Programming and Scripting 1 10-26-2012 11:50 PM
Regular expression (regex) clean up text lawstudent Emergency UNIX and Linux Support 1 02-23-2012 02:55 PM
passing a regex as variable to awk and using that as regular expression for search k_bijitesh Shell Programming and Scripting 7 02-17-2012 01:04 PM
help with simple regex expression schms Shell Programming and Scripting 2 12-16-2010 09:59 PM
Assigning expression value to tcsh variables AB10 Shell Programming and Scripting 2 10-19-2010 08:33 AM
Creating a regex expression bigben1220 Shell Programming and Scripting 3 04-26-2010 02:41 AM
Regular expression / regex substition on Unicode text thomas.hedden UNIX for Advanced & Expert Users 1 02-07-2010 10:55 AM
Regular expression (regex) required shams11 Shell Programming and Scripting 10 01-07-2010 01:27 AM
Need help with arithmetic expression using variables shorehil Shell Programming and Scripting 3 04-18-2009 10:10 PM
Replace if regex on specific column matches expression? EXT3FSCK Shell Programming and Scripting 5 11-25-2008 02:00 PM
variables in regex ? Arcane Shell Programming and Scripting 3 05-31-2006 03:40 AM
All times are GMT -4. The time now is 11:20 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.