LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 02-17-2005, 07:26 PM   #1
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Rep: Reputation: 15
Question awk: /matching/ variables passed with -v


I was introduced to awk today. Yay for me. I'm actually quite enjoying its flexibility, and I think I may very well have found my new best friend.

That is, if we can learn to get along...

I'm trying to use awk to run through logs looking for an IP address, and counting the times its listed, noting the first and last date, and so forth.

I'm passing a variable with -v (the ip address) so that I can awk for it...

The problem is, it matches it verbatim, rather than as a variable...

In other words, if I do:
awk -v ip='192.168.1.101' -f count.awk /var/log/maillog

and it matches based on /ip/, it's matching "ip", not "192.168.1.101".

I'm sure this is ubersimple, but I've only been working with awk for a couple hours now, and this is my first attempt at a non-hello-world-ish application with it.
 
Old 02-17-2005, 07:31 PM   #2
frob23
Senior Member
 
Registered: Jan 2004
Location: Roughly 29.467N / 81.206W
Distribution: Ubuntu, FreeBSD, NetBSD
Posts: 1,449

Rep: Reputation: 47
This is from a program I wrote a while ago for someone.

Code:
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <frob23@yahoo.com> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return. Eric F.
# ----------------------------------------------------------------------------
BEGIN {
  FS="|";
  notfound=1
        }
{if ($3 == id)
  { print newline; notfound=0}
 else {print $0}
} 
END {if (notfound==1)
  print newline
}
Here is the snippet that calls it.

gawk -v id="$ID" -v newline="$LINE" -f updatedata.awk tmpfile >tmp

I hope that helps a little.

Edit: Pay special attention to the bold segments as they should give you an ample hint as to how to do this.

Last edited by frob23; 02-17-2005 at 07:33 PM.
 
Old 02-17-2005, 07:47 PM   #3
aunquarra
LQ Newbie
 
Registered: Jan 2005
Distribution: Slackware 10.1
Posts: 28

Original Poster
Rep: Reputation: 15
Ahh.... Thanks! I didn't think to check that way...

*runs off to invoke the power of awk*
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed/awk for matching & substituition laikos Programming 3 11-22-2006 12:44 PM
Retrieving variables from contents of files using awk or any other bash tool genderbender Programming 8 01-21-2005 04:27 AM
What are the names of the variables passed to konqueror's email client? BluePyre Linux - Software 0 07-04-2004 12:33 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
php variables not being passed on tpat Linux - Software 1 01-11-2003 04:11 PM


All times are GMT -5. The time now is 08:22 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration