LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2012, 07:01 PM   #1
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Rep: Reputation: Disabled
Trouble with syntax while attempting use of regexp in a gawk search


Sorry, guys and gals, if this question is a repeat.. I couldn't find another thread that discussed exactly what I'm trying to do.

Code:
 awk -v cdsl_ip=$cdsl_ip -v cdsl_user=$cdsl_user '
    BEGIN {
      found = "no"
    };
    /\^'$cdsl_ip'\$/ {
I'm trying to search for cdsl_ip, and make sure that it is not part of a larger string. I seem to run into trouble, when I use the variable internal to this instance of awk, cdsl_ip, which is why I used $cdsl_ip, with single quotes. That search works, but returns bad (for my purposes) matches (ie: 209.45.22.11 != 209.45.22.110). Sooo, I'm failing at the regexp tags to mark beginning and end. Any assistance is greatly appreciated. This is gawk 3.1.7, in Solaris 5.9
 
Old 10-18-2012, 09:31 PM   #2
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Quote:
Originally Posted by bspears1 View Post
Sooo, I'm failing at the regexp tags to mark beginning and end.
Perhaps it might work better with an equality than a regexp? Something like..
Code:
$ cat test.file
209.45.22.11	blah
209.45.22.11
209.45.22.13	yada yada

$ cdsl_ip="209.45.22.11"
$ awk 'BEGIN { cdsl_ip="'$cdsl_ip'" ; found="no" }
       $0==cdsl_ip { print ; found="yes" }' test.file
209.45.22.11
Happy with ur solution... then tick "yes" and mark as Solved!
 
1 members found this post helpful.
Old 10-20-2012, 12:06 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
Code:
/\^'$cdsl_ip'\$/ {
When a variable is used directly in a test (without '//'), the value it contains is treated as a regex. But text strings sitting inside '//' fields are not seen or expanded as variables. So the above would never expand correctly.

Also, awk variables are not prepended with "$" in expansion like shell variables. "$" in awk refers to a field number in the input text, so "$variable" would only expand if it contained an integer, and the value seen would be the contents of the field number it refers to.


The secret in the above then is to set the entire regex when you create the variable, and then use it directly in the test part of the expression.

Code:
awk -v cdsl_ip="^$cdsl_ip$' -v cdsl_user="^$cdsl_user$" '
    BEGIN {
      found = "no"
    };
      $0 ~ cdsl_ip {
But as dru8274 said, a simple whole-string equivalence test is preferable anyway. It's both cleaner and more efficient.

Last edited by David the H.; 10-20-2012 at 12:10 PM. Reason: minor code mod
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Gawk - regexp [A-Z] matches [a-z]. How is this possible? b.lundblad@fabula.se Programming 5 08-31-2012 11:15 AM
AND OR NOT and regexp in the search bar Tischbein LQ Suggestions & Feedback 1 10-15-2006 09:32 AM
regexp search for [ wijnands Linux - Newbie 3 06-22-2004 02:15 AM
gawk search parameters dtheorem Linux - General 1 03-10-2004 04:41 PM
How to search for asterisks using gawk dtheorem Linux - General 1 03-05-2004 02:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration