[SOLVED] Pass search results to awk, and use awk output to search other files
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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:
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.
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).
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.
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)
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.