The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 1 Week Ago
baskar baskar is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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 (permalink)  
Old 1 Week Ago
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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 (permalink)  
Old 1 Week Ago
rdcwayx rdcwayx is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 190
Code:
awk  'BEGIN {RS = "exit"} {print $4,$6" "$7,$2,$9,$11,$12,$NF}' OFS="," urfile
  #4 (permalink)  
Old 1 Week Ago
baskar baskar is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 4
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0