LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


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

Rep: Reputation: Disabled
Pass search results to awk, and use awk output to search other files


I am trying to create a shell script that runs a search, passes the results to awk, then uses it's output to run another search and finally print the results. Note that I do not have access to use awk to do the original search (checkuser). I realize that the code below is embarassingly bad.. but I post it here, in hopes that it might clarify what I am trying to get at. Any assistance is greatly appreciated

checkuser $1@acsalaska.net | awk '/framedipaddress/{grep --exclude /var/named/zones/acs.public/nwc.acsalaska.net.db.bak [[:space:]]$2$ /var/named/zones/acs.public/* | awk -F: '{print" Zone routed: ",$1"."$2"."$3"."$4$5}'}'
 
Old 07-19-2012, 10:52 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Firstly, please use [code][/code] tags around your code to keep formatting and make it easier to read

Is checkuser a script? Are we allowed to see its content and what it may be delivering to awk?

Not sure I understand your use of grep (which of course will not work from inside awk (at least not like that))?

It would assist us to help you if we are unable to get ins and outs of checkuser for you to provide an example of its output and then what your overall expected
results would be?
 
1 members found this post helpful.
Old 07-19-2012, 11:55 PM   #3
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
Checkuser is a compiled program that touches systems I don't have permissions on. What I am extracting from it is an IP address. I want to take that address and search all files in a particular directory except for one, then print a line, each time the address is found. Sorry that the code is ugly.. I'm really new. It looks exactly as I typed it in, with one exception; I had a few more spaces in this bit:
Code:
 print"      Zone
 
Old 07-20-2012, 12:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
Sorry that the code is ugly.. I'm really new. It looks exactly as I typed it in, with one exception; I had a few more spaces in this bit
First off, no need to apologise for being new, we all were at some point
If you use the code tags as mentioned it will preserve all your formatting, ie the extra spaces.

1. Are you able to provide some sample output of the checkuser command?

2. /framedipaddress/ - are you actually searching for this string or do the words mean this would contain and ip address?

3. What does $2 refer to and why the $ sign after? (maybe this information is part of question 1 data?)

Again without some idea of inputs and outputs it is difficult to help here. I have some ideas but would be guessing without some of the information above.
 
1 members found this post helpful.
Old 07-20-2012, 02:01 AM   #5
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
Thank you for your time and effort. I'm posting from my phone, currently, so cannot immediately post full output of checkuser. Yes, I am searching for framedipaddress, as it is the first word on the line of checkuser output that contains the IP address. $2 is the second item on that line, and is the address. The $ after it is to indicate that a carriage return follows (so that when I grep for 10.0.0.5, I do not get a match from 10.0.0.50, etc).
 
Old 07-20-2012, 03:26 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
So as I cannot test you will have to use caution with anything supplied:
Code:
checkuser $1@acsalaska.net | awk -F"[ \t:]+" '/framedipaddress/{ip = $2}FILENAME != "nwc.acsalaska.net.db.bak" && $0 ~ ip"$"{print "Zone routed: ",$1"."$2"."$3"."$4$5}' /var/named/zones/acs.public/*
If there is an issue that the string 'framedipaddress' might also be in the files than we can use the set ip value to negate looking at this again:
Code:
!ip && /framedipaddress/...
Let me know if this is at all confusing or you require further explanation?

The link below is always a good resource to have on hand:

http://www.gnu.org/software/gawk/man...ode/index.html
 
1 members found this post helpful.
Old 07-20-2012, 02:43 PM   #7
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
The solution did not work. The search requirements and printed output have changed (only slightly), though what I need still fits the request in the subject of the thread. The below code works, but I need to tell it to only do the grep if the length of $cdslvar is greater than zero. If you can show me how to stop the grep on zero length $cdslvar, that would be great. If you can show me how to do checkuser, piped to a single awk that completes this goal, it would be even better.

Code:
cdslvar=$(checkuser $1@acsalaska.net dsl | awk '/framedipaddress/{print$2}')
grep --exclude /var/named/zones/acs.public/nwc.acsalaska.net.db.bak [[:space:]]$cdslvar[[:space:]] /var/named/zones/acs.public/* | awk '{print"            Zone IP: ",$4,"belongs to",$6}'

Last edited by bspears1; 07-20-2012 at 02:49 PM. Reason: clarification
 
Old 07-20-2012, 05:16 PM   #8
bspears1
LQ Newbie
 
Registered: Jul 2012
Location: Alaska
Posts: 18

Original Poster
Rep: Reputation: Disabled
I looked it over, and over, until I figured out a way to get exactly what I need. If anybody else is looking for a way to pass results from a search to awk, then use that awk's results to set a variable, then use awk to search files for the variable that was set .... here it is (though I'm positive there are more efficient ways to do this)

Code:
cdslvar=$(checkuser $1@acsalaska.net dsl | awk '/framedipaddress/{print$2}')
awk -v cdslvar=$cdslvar '$4==cdslvar && $4!=""{print "            Zone IP: ",$4,"is owned by",$6}' /var/named/zones/acs.public/*.db
Notes:
"cdslvar" is just the name that I picked for my own variable
"checkuser <user> dsl" is a program that we use
"framedipaddress" is a field in the results from checkuser
"/var/named/zones/acs.public/*.db" are the files that I am searching

Last edited by bspears1; 07-20-2012 at 05:25 PM. Reason: clarification
 
Old 07-21-2012, 10:17 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Quote:
The solution did not work.
Unfortunately with such a vague answer it will not really help me to help you. Was there no output? Wrong output? Wrong format?
As you have now also changed the arguments being passed into checkuser it is hard to gauge if the error is due to missing options.

I am happy that you have a solution though
 
  


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
awk search on a variable dazdaz Programming 4 04-15-2012 07:21 AM
Help with awk or sed search. csbushy Linux - Newbie 5 12-14-2011 04:46 AM
[SOLVED] How to get search results to array by awk webhope Programming 5 05-05-2010 12:59 PM
awk search and insert? dyq Linux - Newbie 4 02-17-2010 01:04 AM
awk Question? Search by line, using 2 files? micksul Linux - Software 4 06-06-2007 07:34 AM


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