Perl Split Issue


 
Thread Tools Search this Thread
Top Forums Programming Perl Split Issue
# 8  
Old 05-19-2010
Quote:
Originally Posted by tarken
Hello All -

I am having trouble with the split command in perl. Here is what I am trying to do.

Code:
 75|2455345|2455349|00:00:00|00:00:00|Once|0|Frank Vacation | |
5[4X||-||-
76|2455311|2455312|00:00:00|00:00:00|Once|0|Power Down | |
2|0|0|0|0|0[6X||-||-

I want to only print out the 8th column. So that would be "Frank Vacation" and "Power Down" I need to run this on a file that has thousands of lines like these, and I will always need the 8th column.

Here is how I have been trying to do it:

Code:
#!/usr/bin/perl -w
use warnings;
use strict;
 
open(FH,"data.pl");
 
while (my $line = <FH>) {
my @hash_array = split(/\|/,$line);
print $hash_array[0];
}
 
close(FH);

Thanks for any help provided!

Tarken

You can also use :

Code:
# awk -F\| '{print $8}' file_name

# 9  
Old 05-19-2010
Quote:
Originally Posted by Reboot
You can also use :

Code:
# awk -F\| '{print $8}' file_name


I cant do that, I am adding to perl code that already exists. I must do it in perl without system calls.
# 10  
Old 05-19-2010
I assumed that the input actually looks like this:
Code:
75|2455345|2455349|00:00:00|00:00:00|Once|0|Frank Vacation | |5[4X||-||-
76|2455311|2455312|00:00:00|00:00:00|Once|0|Power Down | |2|0|0|0|0|0[6X||-||-

but
Code:
75|2455345|2455349|00:00:00|00:00:00|Once|0|Frank Vacation | |
5[4X||-||-
76|2455311|2455312|00:00:00|00:00:00|Once|0|Power Down | |
2|0|0|0|0|0[6X||-||-

will spit the same error, because the script handles the input line by line it will fail because in the second line there is no eight column (I guess).

Now the question is was it a copy/paste error or does your input really look like the second case?

Hope I'm clear...
# 11  
Old 05-19-2010
That makes sense. It really does look like that. That could cause some problems. Is it better to try and write a script that will clean it up, and then run this on it. Or try to just fix this script to do what I need?
# 12  
Old 05-19-2010
Quote:
Originally Posted by tarken
Is it better to try and write a script that will clean it up, and then run this on it. Or try to just fix this script to do what I need?
It's a matter of taste, imho; The "fixing script" variant can be done pretty easy by simply skipping the next line after the (first) processed line, but are you sure that every actual line has only one line break?
Again, I hope I was clear.
# 13  
Old 05-19-2010
or ONLY processing lines with more than 8 fields (skipping others) - if it's safe to assume.
# 14  
Old 05-19-2010
Now that you bring this small detail to light. It seems to be a bigger issue. I can post the whole file on here, however, depending on the size of my window, the amount of coloumns will change. I was thinking, do you think it would be better to write a regexp to only print that which has spaces between and print everything on each side until they reach a "|".

Here is an example of the file:

Code:
1|2455011|2455011|5:30:00|19:00:00|Once|0|N233 Outage |Power Outage in N233 |1|0|0|0|0|0[5X||-||-2|2455041|2455041|00:00:00|00:00:00|Once|0|HFGSD Transition |HGFDA. |1|0|0|0|0|0[6X||-||-3|2454998|2454998|00:00:00|00:00:00|Weeks|5|Celeste POC | |6|0|0|0|0|0[2X|2x5x8x11x|-||-4|2455012|2455012|00:00:00|00:00:00|Weeks|5|Nik POC |Nik POC |6|0|0|0|0|0[7X|11x|-||-5|2455015|2455018|00:00:00|00:00:00|Once|0|Celeste Vacation | 

I cant get it to display correctly. But looking through this, can you see where there is actually information? Like Power Outage? Thats the information I need parsed out.
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 and array

Hello, I have the following code: while ($line = <fd_in>) { 126 $line = " " . $line ; 127 print "our_line:$line\n"; 128 @list = split (/\s+/, $line) ; 129 print "after_split:@list\n"; 130 print "$list\t$list\t$list\t$list\t$list\t$list$list\t\n"; 131 $len =... (2 Replies)
Discussion started by: Zam_1234
2 Replies

2. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

3. Shell Programming and Scripting

Perl split function

my @d =split('\|', $_); west|ACH|3|Y|LuV|N||N|| Qt|UWST|57|Y|LSV|Y|Bng|N|KT| It Returns d as 8 for First Line, and 9 as for Second Line . I want to Process Both the Files, How to Handle It. (3 Replies)
Discussion started by: vishwakar
3 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Perl split question

hi, I have a seemingly really stupid question, but here goes! What do you enter into split delimiter to seperate something like this "December 12, 1995" and get December 12 1995 ? thanks (5 Replies)
Discussion started by: ade214
5 Replies

7. 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

8. 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

9. Shell Programming and Scripting

split question perl

I am interested in 2 and 36th fields in this input file. I was wondering if there was a more efficeint way to do this. ($pt1,$bkup_name,$pt3,$pt4,$pt5,$pt6,$pt7,$pt8,$pt9, $pt10,$pt11,$pt12,$pt13,$pt14,$pt15,$pt16,$pt17, ... (7 Replies)
Discussion started by: reggiej
7 Replies

10. Shell Programming and Scripting

perl split function

$mystring = "name:blk:house::"; print "$mystring\n"; @s_format = split(/:/, $mystring); for ($i=0; $i <= $#s_format; $i++) { print "index is $i,field is $s_format"; print "\n"; } $size = $#s_format + 1; print "total size of array is $size\n"; i am expecting my size to be 5, why is it... (5 Replies)
Discussion started by: new2ss
5 Replies
Login or Register to Ask a Question