Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-18-2012, 08:01 PM   #1
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.

 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: != 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, 10:31 PM   #2
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
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..
$ cat test.file	blah	yada yada

$ cdsl_ip=""
$ awk 'BEGIN { cdsl_ip="'$cdsl_ip'" ; found="no" }
       $0==cdsl_ip { print ; found="yes" }' test.file
Happy with ur solution... then tick "yes" and mark as Solved!
1 members found this post helpful.
Old 10-20-2012, 01: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
/\^'$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.

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 01:10 PM. Reason: minor code mod
1 members found this post helpful.


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? Programming 5 08-31-2012 12:15 PM
AND OR NOT and regexp in the search bar Tischbein LQ Suggestions & Feedback 1 10-15-2006 10:32 AM
regexp search for [ wijnands Linux - Newbie 3 06-22-2004 03:15 AM
gawk search parameters dtheorem Linux - General 1 03-10-2004 05:41 PM
How to search for asterisks using gawk dtheorem Linux - General 1 03-05-2004 03:35 PM > Forums > Non-*NIX Forums > Programming

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

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