Solved

detecting lines with "wrong" linebreak

Posted on 2008-10-15
3
179 Views
Last Modified: 2010-03-05
I have a very long xml-file and have now spotted some not wellformed lines. There seem to be linebreaks in some of the tag-content so I have something like:

<Value> bla bla </Value>
<Value> bad
line </Value
<Value> bla bla <Value>

Im looking for a regexp to detect the bad line and chomp it so I will have

<Value> bla bla </Value>
<Value> bad line </Value
<Value> bla bla <Value>

Tried the code below but it didn't work. Chomped all lines.
Beware that some of the good lines might also have a whitespace-char after the last ">"

foreach $line (@lines) {
   print $line;
   if (!($line =~ m/>$/)) {
      print LOG "HIT LINE $line";
      chomp($line);
      print OUT $line;
   } else {
      print OUT $line;
   }  
}


0
Comment
Question by:ventumsolve
3 Comments
 
LVL 17

Accepted Solution

by:
mjcoyne earned 500 total points
Comment Utility
#!/usr/bin/perl -w
use strict;

my @lines = <DATA>;

for (my $i = 0; $i < $#lines; $i++) {
    next if $i == 0;
    if ($lines[$i] !~ /^</) {
        chomp ($lines[$i-1]);
    }
}

print @lines;

__DATA__
<Value> bla bla </Value>
<Value> bad
line </Value
<Value> bla bla <Value>
0
 
LVL 6

Expert Comment

by:RSLE
Comment Utility

$data = join("", @lines);

$data =~ s/\n|\cM//g;           ## remove all line breaks

$data =~ s/\<\/.+?\>/$&\n/g;    ## re-add them

print $data;

Open in new window

0
 

Author Comment

by:ventumsolve
Comment Utility
thanks,
would have liked to see what was wrong with my anchor m/>$/

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Remove Malware code from PHP file 6 53
Perl Tutorial 6 125
Perl for loop for 2000 ms 7 91
Filtering a file to table 9 98
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now