LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl to create an empty line if fields are equal (https://www.linuxquestions.org/questions/programming-9/perl-to-create-an-empty-line-if-fields-are-equal-474496/)

ryedunn 08-16-2006 02:42 PM

Perl to create an empty line if fields are equal
 
I have a rather large pipe delimmited file and I would like to break it down into sections simply by putting an empty line if the second filed is different from the second field in the previous line.

For example if the file is:
1|4847|test|393|somthing
2|4847|another|9|more
3|4847|fo|943|bar
4|4849|blah|93|blah
5|4850|last|384|line
6|4850|sorry|384|ILied


I would like to have a new file which reads:
1|4847|test|393|somthing
2|4847|another|9|more
3|4847|fo|943|bar

4|4849|blah|93|blah

5|4850|last|384|line
6|4850|sorry|384|ILied
Any suggestions you have is greatly appreciated.

xhi 08-16-2006 02:57 PM

if they are sequential maybe something like
Code:

while(<>)
{
  /\d+\|(\d+)\|.+/;
  if($prev != $1)
  {
    print("\n");
  }
  print;
  $prev = $1;
}

a little more logic will remove that newline at the beginning..

demon_vox 08-16-2006 03:14 PM

There are probably betters ways of doing it, but this is the first one I can think of.
Just feed the script with the data with a pipe, a redirection or give the filename as an argument.


Code:

#!/usr/bin/perl

$firstLine = <>;
@fields = split '\|', $firstLine;
$previousField = $fields[1];
print $firstLine;
while(<>) {
    @fields = split '\|', $_;
    $actualField = $fields[1];

    print "\n" if($actualField != $previousField);

    $previousField = $actualField;
    print $_;
}


Cheers!


All times are GMT -5. The time now is 08:52 PM.