![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to group records in a file | trichyselva | Shell Programming and Scripting | 4 | 09-09-2009 04:38 AM |
| command to group records | trichyselva | Shell Programming and Scripting | 7 | 08-11-2009 04:26 AM |
| processing records in a file | CAGIRL | UNIX for Dummies Questions & Answers | 2 | 10-03-2008 04:46 PM |
| AWK Multi-Line Records Processing | RacerX | Shell Programming and Scripting | 10 | 10-18-2007 08:46 PM |
| find and group records in a file | thumsup9 | UNIX for Advanced & Expert Users | 20 | 04-19-2007 05:04 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
|
|
||||
|
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; 1 Week Ago at 11:45 PM.. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|