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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-19-2012, 09:25 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Location: Alaska
Posts: 18
Rep: 
|
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}'}'
|
|
|
07-19-2012, 10:52 PM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
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.
|
07-19-2012, 11:55 PM
|
#3
|
LQ Newbie
Registered: Jul 2012
Location: Alaska
Posts: 18
Original Poster
Rep: 
|
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:
|
|
|
07-20-2012, 12:48 AM
|
#4
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
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.
|
07-20-2012, 02:01 AM
|
#5
|
LQ Newbie
Registered: Jul 2012
Location: Alaska
Posts: 18
Original Poster
Rep: 
|
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).
|
|
|
07-20-2012, 03:26 AM
|
#6
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
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.
|
07-20-2012, 02:43 PM
|
#7
|
LQ Newbie
Registered: Jul 2012
Location: Alaska
Posts: 18
Original Poster
Rep: 
|
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
|
|
|
07-20-2012, 05:16 PM
|
#8
|
LQ Newbie
Registered: Jul 2012
Location: Alaska
Posts: 18
Original Poster
Rep: 
|
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
|
|
|
07-21-2012, 10:17 AM
|
#9
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,031
|
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 11:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|