LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2006, 02:42 PM   #1
ryedunn
Member
 
Registered: Jul 2003
Location: Chicago
Distribution: Fedora, ubuntu
Posts: 459

Rep: Reputation: 30
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.
 
Old 08-16-2006, 02:57 PM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
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..
 
Old 08-16-2006, 03:14 PM   #3
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use awk command to parse fields in a line johnsanty Programming 9 05-25-2006 09:56 PM
create PDF fields and view FDF's PLEASE HELP!!!! klmbrt Linux - Software 4 01-31-2006 07:14 PM
KPPP "Query Modem" returns empty fields ananya Linux - Hardware 2 02-21-2005 10:50 PM
KPPP "Query Modem" returns empty fields ananya Linux - Software 1 02-21-2005 10:39 PM
Can I create a empty /etc/ld.so.preload ? michael_util Linux - Software 1 01-20-2005 06:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration