LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01: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, 05:20 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Give us your code and we'll see how to improve it
 
Old 07-02-2012, 01: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, 03:21 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
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, 06:50 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:54 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
Open Source Consulting | Domain Registration