Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 09-01-2007, 02:15 PM   #1
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Rep: Reputation: 35
Shell variables in awk to define range

I'm trying to use a shell variable in the following awk command.

awk  '/""/,/^};/' named.conf

That will run properly, and output the zone data for

What I want to do is replace "" with a shell variable that will have changing values.

I am having a problem with shell variables in this usage of awk. As a test I did this,

$ awk -v zone=$DOMAIN 'BEGIN { print zone}'
Works fine, however when I try to use it with the initial awk command it doesn't work as expected.

awk -v domain=$ZONE '/domain/,/^};/' named.conf
Where $ZONE is equal to a valid zone in named.conf returns nothing. It is instead looking for the word "domain" and not the variable.

Thanks in advance
Old 09-01-2007, 02:22 PM   #2
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35

awk "/$ZONE/,/^};/" named.conf

awk -v domain="$ZONE" '$0 ~ domain, /^};/' named.conf

Both work.


Those will also match subdomains,

To fix that so they only return an exact match,

awk "/\"$ZONE\"/,/^};/" named.conf


awk -v domain="$ZONE" '$0 ~ "\""domain "\"", /^};/' named.conf

Last edited by fur; 09-01-2007 at 02:56 PM.


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
How to define Environmental Variables in Suse khairkhoah Linux - Software 16 06-08-2006 05:54 AM
Best way to define variables? Yig Linux - Software 3 08-24-2005 05:09 PM
awk: /matching/ variables passed with -v aunquarra Linux - General 2 02-17-2005 06:47 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
how to define variables in the c file of kernel riquelme Programming 2 05-16-2004 07:42 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 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