Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 file AWK

Status
Not open for further replies.

felix001

Technical User
Nov 15, 2008
102
GB
ok, im pretty new to awk, scripting etc etc,
My goal is to search through a config file and get a list of numbers and set all of these to 0, then i want to go through another log file and hit count the existing numbers from the config file so I will get a list of hit counts, even the ones that have a 0 hit count....

Can anyone point me in the right direction...
 
Hi

Search this forum. There were enough similar questions answered over the time to help out any kind of beginner. Without seeing some samples of your input files, we can not suggest a better fitting solution than those.

Feherke.
 
Anyway, what have YOU tried so far and where in YOUR code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
My code has kinda gone down a wrong path, let me explain,

rule=( ` cat /var/gateway.cf | awk ' { print $NF } ' | sed 's|\#||' | sort -n` )
cat /var/log/sg/logfile | awk '/Connection completed/{for(i=1;i<NF;++i)if($i=="Rule"){++rule[$(i+1)];next}}END{for(j in rule)print j,rule[j]}'

Then ive been using a for loop to print the rule array and a while loop to read in the output from awk and print then print it.
The problem is, is that I end up with a list with the rule numbers with a 0 and also the same rule numbers with a hit....

Ive been slowly learning shell scripting, but it looks like i need to do it all in awk... ie set all the numbers from the gateway.cf to 0 then add to them when awking thought the logfile....

So im kinda stuck as I know very very little about awk....good time to learn i suppose...

Thanks for the quick response....
 
A starting point:
awk '
NR==FNR{
rule[$NF]=0;next
}
/Connection completed/{
for(i=1;i<NF;++i)if($i=="Rule"){++rule[$(i+1)];next}
}
END{
for(j in rule)print j,rule[j]
}' /var/gateway.cf /var/log/sg/logfile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
in order to get the list of numbers from the .cf file ill need to cut the last field from the text, but as for the logfile then any line that is has Connection Completed.
So i cant apply the same logic to both files...

Thanks for the input though :)
 
i forgot to mention that the log file is a binary file so we have to use a binary program called flatten to view it.
 
PHV's suggestion is constructed to use diferent logic for each input file; the first statement will only ever be true while processing the first input file, and the second statemetn will noly ever be reached while processing the second (or subsequent) input files.

Just flatten the log file to a temporary file first, and run awk against that temporary file.

Annihilannic.
 
Basically to get the list of numbers from the config file, i have construsted this,

cat /var/lib/sg/gateway.cf | awk ' { print $NF } ' | sed 's|\#||' | sort -n

So im a bit confused how this will fit in to the previously supplied awk code...

 
A stated by feherke previously, without seeing sample input data and the expected output it is difficult for us to offer any advice beyond the suggestions already made.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top