473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem splitting lines in a file

I'm trying to parse a log file using ';' as a newline and then wherever
I find items inside '(..)', indenting those lines thus:
foo;bar(foo;bar(foobar);foo)foobar

becomes

foo
bar(
foo
bar(
foobar
)
foo
)
foobar

What I've got so far is below - the problem is that when there are
nested sets of parentheses, I can't increase the indents accordingly.
Also, I can't work out how to get the closing ')' on a line by itself.

I'm in a bit of a pickle - can anyone help?

What I've got so far follows:
#!/usr/bin/perl -w
use strict;
$|=0;

my @in = <>;
my $tbchr = "\t";
my $tbcnt = 0;

for(@in){
my @ln=split(';', $_);
my $i;
for ($i=0; $i<@ln; $i++){
chomp($ln[$i]);
my @subln;
if ( $ln[$i] =~ /\([^)]/ ) {
@subln = split('\(',$ln[$i]);
my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
'(' }
} elsif ( $ln[$i] =~ /[^(]\)/ ) {
@subln = split('\)',$ln[$i]);
my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
')' }
} else {
@subln = $ln[$i];
}
for(@subln){
my $j;
for($j=0; $j<$tbcnt; $j++){ print $tbchr }
print "$_\n";
$tbcnt++ if /\(/;
$tbcnt-- if /\)/;
}

}
}
Jul 19 '05 #1
1 2528
I'd probably use a counter in a hash to keep track of the ()'s. One key
for the ) and one key for the (. To get the ) on a line by itself I'd do a
=~ s/)/\n)/

Can't remember if you'll need to escape that ) or not.

Mothra wrote:
I'm trying to parse a log file using ';' as a newline and then wherever
I find items inside '(..)', indenting those lines thus:
foo;bar(foo;bar(foobar);foo)foobar becomes foo
bar(
foo
bar(
foobar
)
foo
)
foobar What I've got so far is below - the problem is that when there are
nested sets of parentheses, I can't increase the indents accordingly.
Also, I can't work out how to get the closing ')' on a line by itself. I'm in a bit of a pickle - can anyone help? What I've got so far follows:
#!/usr/bin/perl -w
use strict;
$|=0; my @in = <>;
my $tbchr = "t";
my $tbcnt = 0; for(@in){
my @ln=split(';', $_);
my $i;
for ($i=0; $i<@ln; $i++){
chomp($ln[$i]);
my @subln;
if ( $ln[$i] =~ /([^)]/ ) {
@subln = split('(',$ln[$i]);
my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
'(' }
} elsif ( $ln[$i] =~ /[^(])/ ) {
@subln = split(')',$ln[$i]);
my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .=
')' }
} else {
@subln = $ln[$i];
}
for(@subln){
my $j;
for($j=0; $j<$tbcnt; $j++){ print $tbchr }
print "$_n";
$tbcnt++ if /(/;
$tbcnt-- if /)/;
} }
}

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Luka Milkovic | last post by:
Hello, I have a little problem and although it's little it's extremely difficult for me to describe it, but I'll try. I have written a program which extracts certain portions of my received...
4
by: Hans Almåsbakk | last post by:
Hi, I have a problem which I believe is seen before: Finding the correct pattern to use, in order to split a line correctly, using the split function in the re module. I'm new to regexp, and...
7
by: Jeremy Sanders | last post by:
I have a large string containing lines of text separated by '\n'. I'm currently using text.splitlines(True) to break the text into lines, and I'm iterating over the resulting list. This is very...
1
by: Andy Britcliffe | last post by:
Hi I'm faced with the situation where I could have a single physical file that could contain multiplie XML documents e.g file.txt contains the following: <?xml version="1.0"...
11
by: MM | last post by:
Hi I have never written any C programs before, but it seems that I need to do so now. Hope some of you out there can spend a few minutes and help me by writing a simple example of something...
5
by: Jon | last post by:
I am not too familiar with working with files, so I'd like some advice. I need to write a function for my program that take large text files (> 150 MB) and splits them into several text files of...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
2
by: Fabian Braennstroem | last post by:
Hi, I would like to use re to search for lines in a files with the word "README_x.org", where x is any number. E.g. the structure would look like this: ] I tried to use these kind of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.