Perl split, but ignoring extra delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl split, but ignoring extra delimiters
# 1  
Old 12-06-2010
Perl split, but ignoring extra delimiters

Hi all, I'm going bonkers trying to figure something out that is probably simple for most of you. I have a choice between getting therapy for this or coming here to ask for help. I chose the latter. Smilie

What I'm trying to do is perform a split on a line but only one split. For example, let's say I have the following line:

fieldname:this is a sentence: here is another: here is one more.

and the delimiter I want to split on is ":". I want to split this into two strings. The first will contain the value "fieldname". The second will contain the value "this is a sentence: here is another: here is one more."

So the goal is to do a split but just one per line. Any additional delimiters in the line would be ignored. The only thing I've accomplished so far (repeatedly... lol) is getting the second string set to either "this is a sentence" or nothing at all. Is what I'm trying to do possible?

Thanks very much for any tips you can offer.
# 2  
Old 12-06-2010
One way:

Code:
my ($fName,$sentence,$another,$more) = split /:/, $line;
my $rest = "$sentence".":"."$another".":"."$more";

Another way:

Code:
$line =~ s#:#@#;      This will replace first occurrence of : with @
my ($fName,$rest) = split /@/, $line;

Escape special characters where required.!!
This User Gave Thanks to For This Post:
R0H0N
# 3  
Old 12-06-2010
Thank you! I had been changing the first delimiter in the input file but did not think to try that within the script itself. Doh!

I went with your second option since I don't know how many other delimiters will be in each line. It's working fine now, and I won't have to change any input files.

Thank you again. I really do appreciate it!!
# 4  
Old 12-06-2010
Hi.

There is also a limit that can be placed on split:
Code:
#!/usr/bin/env perl

# @(#) p1	Demonstrate split with limit.

$text = "fieldname:this is a sentence: here is another: here is one more.";

($v1,$v2) = split /:/, $text, 2;

print " v1 = [$v1]\n";
print " v2 = [$v2]\n";

producing:
Code:
% ./p1
 v1 = [fieldname]
 v2 = [this is a sentence: here is another: here is one more.]

See perldoc -f split for more information and details ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 12-06-2010
Very cool, I had no idea it was possible to do that. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Perl] split on : but with extra challenge

Hi, I am looking for a little bit more advanced split. Code: #!/usr/bin/perl -w #-d use strict; my $Line = "value1:{value2a:value2b}:value3:{}:value5:{value6a:value6b}"; my @LineAttributes = split (/:/, $Line); my $TotalLineAttributes = scalar @LineAttributes; print "Line:... (4 Replies)
Discussion started by: ejdv
4 Replies

2. Shell Programming and Scripting

Split using two delimiters

I'm trying to do a split using two delimiters. The first delimiter is ": " (or we could call it :\s). The second is "\n". How can or these delimiters so I can toss the values into an array without issue? I tried @array = split /:\s|\n/, $myvar; This doesn't seem to be working. Any an... (3 Replies)
Discussion started by: mrwatkin
3 Replies

3. Shell Programming and Scripting

Perl split and join

Hi, I have tried the split and join functions but stuck with unexpected results. Any help appreciated. I pass multiple values at command line like perl test.pl -type java,xml. This works good for me but i am not sure how to print it in the required format. Here is the code i tried:... (4 Replies)
Discussion started by: nmattam
4 Replies

4. Shell Programming and Scripting

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

5. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (0 Replies)
Discussion started by: castle
0 Replies

6. Homework & Coursework Questions

PERL split function

Hi... I have a question regarding the split function in PERL. I have a very huge csv file (more than 80 million records). I need to extract a particular position(eg : 50th position) of each line from the csv file. I tried using split function. But I realized split takes a very long time. Also... (1 Reply)
Discussion started by: castle
1 Replies

7. Shell Programming and Scripting

PERL cgi script... extra character driving me crazy

I'm using a PERL cgi script that uses rrdtool to make graphs. I can't get the syntax correct to use a degree sign (alt+0176 like this °) and also using a variable. If I use single quotes, I can't call the variable. If I use double quotes, there is an extra symbol (Â) before the ° which goes... (2 Replies)
Discussion started by: audiophile
2 Replies

8. Shell Programming and Scripting

Use split function in perl

Hello, if i have file like this: 010000890306932455804 05306977653873 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC30693599000 30971360000 ZZZZZZZZZZZZZZZZZZZZ202011302942311 010000890306946317387 05306977313623 0520080417010520ISMS SMT ZZZZZZZZZZZZZOC306942190000 30971360000... (5 Replies)
Discussion started by: chriss_58
5 Replies

9. UNIX for Advanced & Expert Users

Split Command in Perl

Hi, I have to split a line of the form 1232423#asdf#124324#54534#dcfg#wert#rrftt#4567 into an array in perl. I am using @fields; @fields=split('#',$line); if($fields eq "1") But this is not working. By using the syntax, the statements in "if" are never executed. Please help.... (9 Replies)
Discussion started by: rochitsharma
9 Replies

10. Shell Programming and Scripting

split to array in perl

Collegues I have flat file in the following format. 137 (NNP Kerala) (NNP India) 92 (NN Rent) (NN Range) 70 (NNP Thiruvananthapuram) (NNP Kerala) 43 (NNP Tourist) (NNP Home) 40 (NNP Reserve) (NNP Now) 25 (SYM @) (NN hotelskerala) 25 (NNP Thiruvananthapuram-695001) (NNP Kerala) 23 (NN... (3 Replies)
Discussion started by: jaganadh
3 Replies
Login or Register to Ask a Question