Solved

Split

Posted on 2000-04-23
14
175 Views
Last Modified: 2010-03-05
Is there anyway to make 'split' work just like 'cut' of shell script?

I would like get data from column 2 and 4.

col1   col2     col3     col4  
xxx    xxx      xxx      xxxx
ddd    ddd      ddd      dddd
fff    ddd      sss      rrrr

Thanks.
0
Comment
Question by:sata
  • 5
  • 5
  • 4
14 Comments
 
LVL 4

Expert Comment

by:adam923
ID: 2742157
open(INFILE, "<textfile");
while(<INFILE>){
  @cols = split;
   # do stuff with $cols[1] and $cols[3] (columns 2 and 4)
}
close INFILE;
0
 
LVL 84

Expert Comment

by:ozo
ID: 2742269
$cut = join'\t',(split)[1,3]; # cut -f2,4
0
 
LVL 1

Author Comment

by:sata
ID: 2742514
Hi,

sorry that I'm not very good with Perl,
I would like to ask, isn't split's syntax something like 'split(@array)? What does "join'\t',(split)[1,3]" means?

Thank you very much for the comments...

0
 
LVL 84

Expert Comment

by:ozo
ID: 2742530
perldoc -f split
=item split /PATTERN/,EXPR,LIMIT

=item split /PATTERN/,EXPR

=item split /PATTERN/

=item split

Splits a string into an array of strings, and returns it.  By default,
empty leading fields are preserved, and empty trailing ones are deleted.
0
 
LVL 4

Expert Comment

by:adam923
ID: 2742654
To explain my example, split in array context with no arguments splits the default variable ($_ which now refers to the current line of text) into fields separated by whitespace.
0
 
LVL 1

Author Comment

by:sata
ID: 2742897
Am I right to say that the data
col1   col2     col3     col4    
xxx    xxx      xxx      xxxx
ddd    ddd      ddd      dddd
fff    ddd      sss      rrrr

will become a long string, something like:

col1 col2 col3 col4 xxx xxx xxx xxxx ddd ddd ddd dddd

if I need to access the 'xxx' data of column1, I need to (after split) specify "array[4]" to retrive the data?

So it doesn't really work like 'cut', isn't it?
0
 
LVL 4

Expert Comment

by:adam923
ID: 2742907
in my example the data is delt with one line at a time
one could expand this to keep track of each column separately

open(INFILE, "<textfile");
while(<INFILE>){
     @cols = split;
     push @col2, $cols[1];
     push @col4, $cols[3];
}
close INFILE;

Now the array @col2 = (col2, xxx, ddd, ddd) and the array @col4 = (col4, xxxx, dddd, rrrr);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:sata
ID: 2742997
Adjusted points from 50 to 100
0
 
LVL 1

Author Comment

by:sata
ID: 2742998
you are using a file as your input, so if I used a variable:

e.g.

$list1 = `ls -l`;

while($list1) {
     @cols = split;
     push @col2, $cols[1];
     push @col4, $cols[3];
}

will the system still take in the data line by line?
Sorry for all the trouble caused.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2743006
#if you used an array:
@list1 = `ls -l`;
foreach( @list1 ){
#it would take the data line by line.
}
#but if you want to take columns from `ls -l`, you might consider using the built in stat function.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 2743014
#or
foreach( split/\n",$list1 ){
    @cols = split;
    push @col2, $cols[1];
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 2743015
foreach( split/\n/,$list1 ){
0
 
LVL 1

Author Comment

by:sata
ID: 2743755
Adam has been a great help too, is there anyway I can give points to him too?
0
 
LVL 4

Expert Comment

by:adam923
ID: 2743990
you could post another ? but don't bother, thanks for the offer though
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now