Processing records as group - awk

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Infrastructure Monitoring Processing records as group - awk
# 1  
Old 11-11-2009
Processing records as group - awk

I have a file has following records

policy glb id 1233 name Permit ping from "One" to "Second" "Address1" "Any" "ICMP-ANY" permit
policy id 999251
service "snmp-udp"
exit
policy glb id 1234 name Permit telnet from "One" to "Second" "Address2" "Any" "TCP-ANY" permit
policy id 1234
service "tcp"
exit

I want to generate a record for each glb id. exit is the end for each policy id.

the expected Output is:

#id#name,type,from,to,Address,service
1233,Permit ping,glb,One,Second,Address1,snmp-udp
1234,Permit telnet,glb,One,Second,Address2,snmp-tcp

Thanks in advance
# 2  
Old 11-11-2009
Code:
local $/;
my $str=<DATA>;
my @tmp=split(/(?<=exit)\n/,$str);
foreach(@tmp){
	if(/.*id\s*([0-9]+)\s*name\s*(.*)\sfrom\s*"([^"]*)"\s*to\s*"([^"]*)"\s*"([^"]+)".*service\s*"([^"]+)"/msx){
		print "$1,$2,$3,$4,$5,$6\n";
	}
}
__DATA__
policy glb id 1233 name Permit ping from "One" to "Second" "Address1" "Any" "ICMP-ANY" permit 
policy id 999251
service "snmp-udp"
exit
policy glb id 1234 name Permit telnet from "One" to "Second" "Address2" "Any" "TCP-ANY" permit 
policy id 1234
service "tcp"
exit

# 3  
Old 11-12-2009
Code:
awk  'BEGIN {RS = "exit"} {print $4,$6" "$7,$2,$9,$11,$12,$NF}' OFS="," urfile

# 4  
Old 11-12-2009
Thanks for quick update.

I'm working with a file that does not have fixed field numbers, some records have additional name and value. I want to make single line from each set ex: from policy to exit, then I want to print the value of id, name, address(multiple addresses) by finding the pattern (id, name ..etc)

awk '/policy/,/exit/{ if($0~/exit/) ORS="\n"; else ORS=" "; print}' input > tmp

I want to read tmp and find next word (value) of matched pattern (id, name, etc)



first record
policy glb id 1233 name Permit ping from "One" to "Second" "Address1" "Any" "ICMP-ANY" permit
policy id 1233
service "snmp-udp"
exit
second record
policy id 1234 name Permit ping from "One" to "Second" address "Address1" "Any" "ICMP-ANY" permit
policy id 1234
address "xyzabc"
address "12345"
service "snmp-udp"
exit

Expected output:

id name from to address service
1234 Permit Ping One Second Address1 snmp-udp
1234 Permit Ping One Second xyzabc snmp-udp
1234 Permit ping One Second 12345 snmp-udp

Last edited by baskar; 11-12-2009 at 11:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To group records in the file in UNIX

Hi All, I am using RHEL 6.9. I got a requirement to group the records in a file.The file content as shown below. #### FAILED JOBS IN XXX ##### 1> ABCD failed in the project XXX 2> HJK Job is in compiled state in the project XXX 3> ILKD failed in the project XXX 4> DFG failed in the... (5 Replies)
Discussion started by: ginrkf
5 Replies

2. UNIX for Beginners Questions & Answers

Awk: group multiple fields from different records

Hi, My input looks like that: A|123|qwer A|456|tyui A|456|wsxe B|789|dfgh Using awk, I am trying to get: A|123;456|qwer;tyui;wsxe B|789|dfgh For records with same $1, group all the $2 in a field (without replicates), and all the $3 in a field (without replicates). What I have tried:... (6 Replies)
Discussion started by: beca123456
6 Replies

3. Shell Programming and Scripting

Looping through records in db and processing them in UNIX

Hi, I want to read multiple records from oracle database table and then based on each record I need to do some file modifications in UNIX. I'm not sure how do I process each record from DB in UNIX.Below is the code snippet. sqlplus user/pwd@DEV for i IN (select * from table) loop -- for... (2 Replies)
Discussion started by: prats_7678
2 Replies

4. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

5. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

6. Shell Programming and Scripting

how to group records in a file

hi, I have records like this D127@dm.com,127,569,BRAD,25/08/2009 23:59 D127@dm.com,127,569,BRAD,25/08/2009 23:59 D159@dm.com,159,1170,DAVE,25/08/2009 23:59 D159@dm.com,159,1181,HALE,25/08/2009 23:59 D393@dm.com,393,1209,CAPIT,25/08/2009 23:59 D457@dm.com,457,571,NORTT,25/08/2009 23:59... (4 Replies)
Discussion started by: trichyselva
4 Replies

7. Shell Programming and Scripting

command to group records

hi, i have records like this supplier,product, persons involved 10,150,ravi@yahoo.com 30,200,ravi@yahoo.com 20,111,payal@gmail.com 40,211,ravi@yahoo.com i want to write a command which displays values like this 10,30,40,ravi@yahoo.com for ravi@yahoo.com if there are n number of... (7 Replies)
Discussion started by: trichyselva
7 Replies

8. UNIX for Dummies Questions & Answers

processing records in a file

Hi, I have a file that contains some records that I would like to process each line that I am interested in. My goal is to create a new file that contains only the rest_of_line for each line that matches. It has the following File Format command, platform, rest_of_line" I am searching the... (2 Replies)
Discussion started by: CAGIRL
2 Replies

9. Shell Programming and Scripting

AWK Multi-Line Records Processing

I am an Awk newbie and cannot wrap my brain around my problem: Given multi-line records of varying lengths separated by a blank line I need to skip the first two lines of every record and extract every-other line in each record unless the first line of the record has the word "(CONT)" in the... (10 Replies)
Discussion started by: RacerX
10 Replies
Login or Register to Ask a Question