Solved

Split

Posted on 2000-04-23
14
177 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Existing perl code to be changed for ftp to sftp handling 14 194
binary to char / hexadecimal 5 113
Strange perl issue 6 128
Excel to CSV conversion with specific columns 5 82
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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