- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Using PErl to loop through a file
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2005 10:17 PM
тАО08-02-2005 10:17 PM
Using PErl to loop through a file
The file consists of hundreds of lines, with records that begin with
_obj_id: alarmRecordId I 340
and ends with
_end_block:
Well that is the range of each record that I want.
However the portion that starts with block and ends with end_block is in more than one line, and that is where I am getting stumped.
I feed each line into the array. When I get to the block portion I have no idea how to tackle it. A sample of the file is attached.
I need to output in a flat file format where each record will be converted to a one line.
Typical Output for first record
alarmRecordId = 340: COMPID = NI EM MP2RPB01 LP 5 E1 3: severity = E critical: commentData S Loss of Frame condition has been detected (dl1LofAlarm). Check the statistics attributes and configuration of the far-end.
This should be in one line from the 1st record provided in the tmp1 file
Can anyone help
Regards
Steve
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2005 10:48 PM
тАО08-02-2005 10:48 PM
Re: Using PErl to loop through a file
awk -F: 'BEGIN { str="" }
block == 1 { str = str$1 }
$1 == "_obj_id" { str = str$2 ; block = 0 }
$1 == "_attr" { str = str" : "$2 }
$1 == "_block" { block = 1 ; str = str" : "$2 }
$1 == "_end_block" { print str"\n" ; str = "" }
' yourfile
This should give you a start.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2005 11:10 PM
тАО08-02-2005 11:10 PM
Re: Using PErl to loop through a file
--8<---
open my $file, "< file.xxx" or die "file.xxx: $!\n";
local $/ = "_end_block:\n";
while (<$file>) {
# process the block
}
close $file;
-->8---
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-02-2005 11:45 PM
тАО08-02-2005 11:45 PM
Re: Using PErl to loop through a file
e.g. in your parsing loop
while (
if (/^_block/.../^_end_block/) {
# do something on matched line
}
}
If the files you parse are exceptionally large I would strongly dissuade you from pushing their contents in an array, as it consumes too much memory.
Better operate on the content linewise as in the example given by procura.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2005 04:44 AM
тАО08-03-2005 04:44 AM
Re: Using PErl to loop through a file
But if I loop through a file line by line, how can the I use the separator block, as each line is read in a new array.
I am not sure how you would use the block portion as a separator.
Regards
steve
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2005 05:41 AM
тАО08-03-2005 05:41 AM
Re: Using PErl to loop through a file
local $/ = "_end_block:\n";
while (<>) {
my @lines = split m/\n+/, $_;
# process line by line in block
foreach my $line (@lines) {
# these are the single lines
}
}
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2005 06:34 PM
тАО08-03-2005 06:34 PM
Re: Using PErl to loop through a file
As for procura's example I should of course have correctly said blockwise instead of linewise.
But comparing the No. of bytes per block compared to the whole file, that's still more resource friendly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2005 10:05 PM
тАО08-03-2005 10:05 PM
Re: Using PErl to loop through a file
_obj_class: alarmRecord
_obj_id: alarmRecordId I 340
_attr: time D 2005 07 05 13 54 46
_attr: compId NI EM MP2RPB01 LP 5 E1 3
_attr: rawState E OOS
_attr: compCriticality I 40
_attr: alarmType E communications
_attr: faultCode S 70115000
_attr: severity E critical
_attr: probableCause E lossOfFrame
_attr: notificationId I 83886094
_attr: originatorClass E passport
_attr: active I 1
_attr: customerId I 0
_attr: event E set
_block: _attr commentData S
Loss of Frame condition has been detected (dl1LofAlarm).
Check the statistics attributes and configuration of the far-end.
SO THAT I CAN CALL EACH KEY-VALUE BY NAME.
WOULD THIS BE POSSIBLE.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2005 10:27 PM
тАО08-03-2005 10:27 PM
Re: Using PErl to loop through a file
while (<>) {
my %hash = (/^(\w+):\s*(.*)/mg);
# $hash{_obj_id} = "alarmRecordID I 340"
}
Simple enough?
Enjoy, Have FUN! H.Merijn