LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2012, 02:16 AM   #1
lakssreedhar
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
perl code to split multiple lines of data into 2columns


Hi
I have a data somewhat in the below format
{MCL}who is this.{/MCL}
{RP}what a shame.{/RP}
I would like to write a code to split the file as follows
who MCL
is o
this o
. MCL
and similar for all lines.The code i developed using split function just splits the above in a single column.awaiting reply and thanks in advance
 
Old 06-30-2012, 06:20 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Give us your code and we'll see how to improve it
 
Old 07-02-2012, 02:56 AM   #3
lakssreedhar
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have done something llike this.But the output is not in the expected format.
#!/usr/bin/perl

while(<>)
{

#replace multiple .'s by a single .
$_ =~ s/\.\.+/ /ig;

#split multiple sentences into an array.
next if($_ eq undef);
@arr=split(/\t/,$_);
print"$arr[0]\t$arr[1]\n";
}
 
Old 07-02-2012, 04:21 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 8,514

Rep: Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436Reputation: 2436
just please give us a sample input and output (a bit more, if possible) and also please use [code][/code] to keep formatting of your code.

try the following:
1. use #!/usr/bin/perl -w at the beginning ( so add -w )
2. add this as second line: use strict;
these will ensure you will write better code
3. the line: next if($_ eq undef); is meaningless, $_ always has value (because otherwise the while loop will be terminated)
4. you ought to need check if @arr has 2 elements
 
Old 07-02-2012, 07:50 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
In addition to the excellent advice above
Code:
#replace multiple .'s by a single .
None of your input has multiple successive '.'s

Code:
#split multiple sentences into an array.
By default, Perl is line oriented, so I think you mean words not sentences here. Also, your code implies tabs between the 'words'; is that right?

Also, please use code tags for posting code https://www.linuxquestions.org/quest...do=bbcode#code
 
  


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
Split single line into multiple lines with 3 column each udiubu Programming 4 10-28-2011 12:20 AM
Perl vs. bash code - number of lines noir911 Programming 11 10-07-2011 01:35 PM
Split data of text file into mysql with perl koscek Programming 1 11-01-2007 11:26 AM
UNIX shell script: split long command on multiple lines loopoo Linux - Newbie 2 10-23-2006 10:34 AM
Perl: Binary data to be split into ints, shorts and floats ppanyam Programming 1 12-08-2005 06:27 PM


All times are GMT -5. The time now is 09:16 PM.

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