awk variables in regex expression ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variables in regex expression ?
# 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 or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies

2. UNIX for Beginners Questions & Answers

Regex Expression Replace

I have a XML file where there is a tag with like <wd:address_line_1>1234 Street</wd:address_line_1> I want to replace the values "1234 Street" with "Test Data". Different people have different address lines and i want to replace with a fixed value to mask the file. I was trying to use sed... (7 Replies)
Discussion started by: dr46014
7 Replies

3. Shell Programming and Scripting

awk regex expression works in AIX but not in Linux

I have following expression: echo "Sun 12 Jul BST 2014\nSun 12 Jul 2014\nSun 12 Jul IS 2014" | awk '/(Sun)+( 12)+( Jul )+({3} )?(2014)/{print;}' I ran above code in AIX box and output is as follows Sun 12 Jul BST 2014 Sun 12 Jul 2014 I ran above code in Linux box and output is as... (8 Replies)
Discussion started by: kamlesh_pradhan
8 Replies

4. Shell Programming and Scripting

Hi im new to bash scripting I want to know what does the regex expression do ??

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi (1 Reply)
Discussion started by: kevin298
1 Replies

5. Emergency UNIX and Linux Support

Regular expression (regex) clean up text

Hi, Server - MEDIAWIKI - MYSQL - CENTOS 5 - PHP5 I have a database import of close to a million pages into my wiki, mediawiki site, the format that were left with is not pretty, and I need to find a way to clean this up and present it nicely... I think regex is the best option as I can... (1 Reply)
Discussion started by: lawstudent
1 Replies

6. Shell Programming and Scripting

passing a regex as variable to awk and using that as regular expression for search

Hi All, I have a sftp session log where I am transferring multi files by issuing "mput abc*.dat". The contents of the logfile is below - ################################################# Connecting to 10.75.112.194... Changing to: /home/dasd9x/testing1 sftp> mput abc*.dat Uploading... (7 Replies)
Discussion started by: k_bijitesh
7 Replies

7. Shell Programming and Scripting

help with simple regex expression

I am trying to grep the following line in a file using a bash shell: (..) admin1::14959:::::: (..) It works with the following expression (as expected) # cat file | grep ^*:: admin1::14959:::::: but it does not work with (not expected) # cat /etc/shadow | grep ^+:: I assume the... (2 Replies)
Discussion started by: schms
2 Replies

8. Shell Programming and Scripting

Creating a regex expression

Good morning all!! In my code I and looking through file /etc/syslog.congf and printing every line that has /var/log in it. I need to turn the if 9$line) into a regex code instead. #!/usr/bin/perl @file= 'cat /etc/syslog.conf'; //when foreach $line (@file){ if ($line =~... (3 Replies)
Discussion started by: bigben1220
3 Replies

9. Shell Programming and Scripting

Regular expression (regex) required

I want to block all special characters except alphanumerics.. and "."(dot ) character currently am using // I want to even block only single dot or multiple dots.. ex: . or .............. should be blocked. please provide me the reg ex. ---------- Post updated at 05:11 AM... (10 Replies)
Discussion started by: shams11
10 Replies

10. Shell Programming and Scripting

Replace if regex on specific column matches expression?

I am attempting to convert rewrite rules to Nginx, and since due to the mass amount of rewrites we must convert, I've been trying to write a script to help me on a specific part, easily. So far I have this: rewrite ^action/static/(+)/$ staticPage.php?pg=$1&%$query_string; What I want done... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies
Login or Register to Ask a Question