Awk Help Needed * Please Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Help Needed * Please Help
# 1  
Old 10-24-2008
Awk Help Needed * Please Help

I am new with awk scripting. I am trying to do the following:

1.) Parse various files with multiple extensions (.csv, .txt, .nbe)
2.) Print a field=>Sort via Uniq
3.) I am trying print a line that shows the total
4.) Output to a console and text file

* Will I be able to read via 'cat' a group of files in a directory.

awk
BEGIN {

if ( $0 ~ /|/) {
FS=":"; }{
print "Tool #1 Scan Results"
print --------------------
print $2 | Sort | uniq
print "Total IP Count: $count" # Not sure how to come up with the $count of IPs Smilie
}
else ($0 ~ /Interesting/) {
print "Tool #2 Scan Results"
print ------------------
print $4 | Sort | uniq
print " Total IP Count: $count" # Not sure how to come up with the $count of IPs Smilie
}
}
# 2  
Old 10-24-2008
Could you post sample input and an example of the desired output?
# 3  
Old 10-24-2008
Sure Thanks.

.NBE File Format

Tool #1 Raw Results
results|xxx.xxx.3|xxx.xxx.3.85|microsoft-ds (445/tcp)|11119|

.txt File Format

Tool #2 Raw Results
Interesting xxx.xxx.3 microsoft-ds (445/tcp) xxx.xxx.3.85 11119 x

Desire Output:

Tool #1 Scan Results
-----------------------
xx.xx.xx.23
xx.xx.xx.24
xx.xx.xx.24

Total IP Count: 3

Same for Tool #2

Thanks!
# 4  
Old 10-24-2008
... Also, is it possible to create this shell script (bash) to parse all files in the directory? Thanks in advance for your help.
# 5  
Old 10-24-2008
Do you want to count the unique IPs accross different files or you want the count and unique per file? Could you please post a bigger part of at least three files (change all sensible data IPs etc).

Could you also provide the platform (uname -a) and the awk version (awk --version) ?

Last edited by radoulov; 10-24-2008 at 04:13 PM..
# 6  
Old 10-24-2008
I would like the IP count for each file not combine.

Total #1 Raw Results (.NBE) (I just need the IP address)
results|xx.xx.3|xx.xx.3.85|microsoft-ds (445/tcp)|11119|Security Note
results|xx.xx.3|xx.xx.3.85|ms-wbt-server (3389/tcp)|22964|Security Note
timestamps||xx.xx.3.5|host_end|Fri Sep 26 14:20:20 2008
results|xx.xx.3|xx.xx.3.245|ntp (123/udp)|10884|Security Note
results|xx.xx.3|xx.xx.3.245|general/tcp|19506|Security Note

Tool #2 Raw Results (I just need the IP addresses)

Interesting ports on xx.xx.1.99:
Not shown: 1710 closed ports
PORT STATE SERVICE VERSION

All 1715 scanned ports on xx.xx.1.100 are filtered
Too many fingerprints match this host to give specific OS details

All 1715 scanned ports on xx.xx.1.100 are filtered
Too many fingerprints match this host to give specific OS details

Interesting ports on xx.xx.1.99:
Not shown: 1710 closed ports
PORT STATE SERVICE VERSION

Interesting ports on xx.xx.1.99:
Not shown: 1710 closed ports
PORT STATE SERVICE VERSION

******

I was using the following commands individually:

cat *.nbe | awk -F"|" '{print$3}' | sort | uniq
cat *.txt | grep Interesting | awk '{ print $4}'|sort|uniq

I would like a script for this and additional information. I perfer a bash shell for this and a summary as well. Later, I would like to add one additional file format.

Thanks!!!!!!!!!
# 7  
Old 10-24-2008
You can try something like this (the code may need some corrections).
Just copy/paste the code in your terminal.

(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'BEGIN { cmd = "sort -t. -k1n -k2n -k3n -k4n " }
FNR == 1 { 
  FS = FILENAME ~ /\.nbe$/ ? "|" : OFS 
  if (f) {
    for (k in ip) {
	  print k | cmd
      c++
	  }	  
	close(cmd)
    printf "\nTotal IP Count: %d\n\n", c
	c = f = 0
	split("", ip)
	}
	print FNR == 1 ? $0 RS : RS $0 RS
	next
	}
(FILENAME ~ /\.nbe$/ && $2)|| /Interesting/ { 
  /Interesting/ && sub(/:$/,"")
  ip[FILENAME ~ /\.nbe$/ ? $2 : $NF]
  f = 1  
  }
END  {
  if (f) {
    for (k in ip) {
	  print k | cmd
      c++
	  }	  
	close(cmd)
    printf "\n\nTotal IP Count: %d\n\n", c
	}
}' *.nbe *.txt


Last edited by radoulov; 10-24-2008 at 05:09 PM.. Reason: correction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - help needed please

Hi.. have a file as below, appreciate if someone can help on this 143|500| 10| 23353 22131 23355 23354 23358 23352 23357 23350 23349 23351| RAID5 213|1008| 9| 22419 22412 221 22413 22414 22416 22417 22415 22418| RAID6 1088|500| 5| 22243 22240 22244 22242 22241| RAID5 322|1200| 12|... (6 Replies)
Discussion started by: richard0@rediff
6 Replies

2. Shell Programming and Scripting

awk help needed

Hi Everyone, i have following in my file 1 2 3 4 5 6 . . 100 and now i want the output as 1 4 7 ..........so on..............97 100 (10 Replies)
Discussion started by: zozoo
10 Replies

3. Shell Programming and Scripting

awk help needed

Hi Experts, I have a file (file 1) with several columns and I need to create 2 files based on the data of 20th column of file 1. Criteria 1 : If the 20th field of file1 is empty , copy the entire records to file 2. I am successfully able to do this with the following awk code : awk... (2 Replies)
Discussion started by: nua7
2 Replies

4. Shell Programming and Scripting

awk help needed

Hi, i have input records as shown below. 4097,Probe3,G10,255,05/17/2011 12:44:03:185,NULL,05/17/2011 12:39:03:180,05/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

5. UNIX for Dummies Questions & Answers

help needed for awk

Dear all, I am new to use unix. I run the following command and got the error. Anyone knows how should I modify the command. Thanks a lot! $ for chr in 'seq 1 23'; do awk 'BEGIN {print "T","pheno";}{print "M",$2}' out_${chr}.map > dat_${chr}.dat; done error message:... (2 Replies)
Discussion started by: forevertl
2 Replies

6. Shell Programming and Scripting

Awk Help needed

hi, I have input file woth records as shown below OCSMRC_OK,7057348733,+0.00,0,18/05/2010 23:42:19,BellMobility,302610000918553,0006056099,B30,686505,686505,OCS_MRC,+49.14,0,0 ,0,0, OCSPPKB_NOK,4163460120,+1.25,0,18/05/2010... (4 Replies)
Discussion started by: raghavendra.cse
4 Replies

7. UNIX for Dummies Questions & Answers

Awk help needed

I have a log file monitor script that checks through a log file for a string. I use awk to search the log file, starting at the last checked line, for the specified string and then output the count and the last row number checked. The part of the script that does all the work is here: set --... (6 Replies)
Discussion started by: mglenney
6 Replies

8. Shell Programming and Scripting

help needed in awk

Hi , i have a file a.txt like this: far near veryfar toonear typeset var1=veryfar to extract the text between two strings i use the following command : awk '/far/,$veryfar/' a.txt its not working can nyone tell pls whats wrong in it ? i doubt can we use variable in awk like this... (3 Replies)
Discussion started by: santosh1234
3 Replies

9. Shell Programming and Scripting

awk help needed

How do I alter this command so that it prints only the second comma delimited field from line number 3? Secondly, how do you redirect the output to a variable called TEST? Thanks (cat BATCH007.TXT | awk 'BEGIN { FS = "," } ; {print $2 }') (5 Replies)
Discussion started by: ddurden7
5 Replies

10. Shell Programming and Scripting

awk help needed

I am trying to write a script that will parse out the e-mail address of a person from the name of a file in a directory. Example: filename is: /home/myname/first.middle.last@email.com.xls I want to extract just the email address and mail the file to that address. I want to send the... (6 Replies)
Discussion started by: Drenhead
6 Replies
Login or Register to Ask a Question