Solved

Split

Posted on 2000-04-23
14
174 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
Comment Utility
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
Comment Utility
$cut = join'\t',(split)[1,3]; # cut -f2,4
0
 
LVL 1

Author Comment

by:sata
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:sata
Comment Utility
Adjusted points from 50 to 100
0
 
LVL 1

Author Comment

by:sata
Comment Utility
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
Comment Utility
#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
Comment Utility
#or
foreach( split/\n",$list1 ){
    @cols = split;
    push @col2, $cols[1];
}
0
 
LVL 84

Expert Comment

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

Author Comment

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

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

9 Experts available now in Live!

Get 1:1 Help Now