LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Pass search results to awk, and use awk output to search other files (https://www.linuxquestions.org/questions/linux-newbie-8/pass-search-results-to-awk-and-use-awk-output-to-search-other-files-4175417690/)

bspears1 07-19-2012 08:25 PM

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}'}'

grail 07-19-2012 09:52 PM

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?

bspears1 07-19-2012 10:55 PM

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

grail 07-19-2012 11:48 PM

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.

bspears1 07-20-2012 01:01 AM

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).

grail 07-20-2012 02:26 AM

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

bspears1 07-20-2012 01:43 PM

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}'


bspears1 07-20-2012 04:16 PM

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

grail 07-21-2012 09:17 AM

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 :)


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