[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Split

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
sata
Asked:
sata
  • 5
  • 5
  • 4
1 Solution
 
adam923Commented:
open(INFILE, "<textfile");
while(<INFILE>){
  @cols = split;
   # do stuff with $cols[1] and $cols[3] (columns 2 and 4)
}
close INFILE;
0
 
ozoCommented:
$cut = join'\t',(split)[1,3]; # cut -f2,4
0
 
sataAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ozoCommented:
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
 
adam923Commented:
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
 
sataAuthor Commented:
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
 
adam923Commented:
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
 
sataAuthor Commented:
Adjusted points from 50 to 100
0
 
sataAuthor Commented:
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
 
ozoCommented:
#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
 
ozoCommented:
#or
foreach( split/\n",$list1 ){
    @cols = split;
    push @col2, $cols[1];
}
0
 
ozoCommented:
foreach( split/\n/,$list1 ){
0
 
sataAuthor Commented:
Adam has been a great help too, is there anyway I can give points to him too?
0
 
adam923Commented:
you could post another ? but don't bother, thanks for the offer though
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now