LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-01-2007, 02:15 PM   #1
fur
Member
 
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.

Code:
awk  '/"domain.com"/,/^};/' named.conf

That will run properly, and output the zone data for domain.com.

What I want to do is replace "domain.com" 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,

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

Code:
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
fur
Member
 
Registered: Dec 2003
Distribution: Debian, FreeBSD
Posts: 310

Original Poster
Rep: Reputation: 35
Nevermind,

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

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

Both work.

EDIT:

Those will also match subdomains,

To fix that so they only return an exact match,

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

or

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

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


Reply



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

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

All times are GMT -5. The time now is 04: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
Open Source Consulting | Domain Registration