1755839 Members
4756 Online
108838 Solutions
New Discussion юеВ

How to split in Perl??

 
Rajesh SB
Esteemed Contributor

How to split in Perl??

Hi,

I want convert below shell script statement into perl using split// or tr//.

echo "$i"| cut -d ">" -f2 | tr -d "',"

Thanks & Regards,
Rajesh


3 REPLIES 3
VK2COT
Honored Contributor

Re: How to split in Perl??

Hello,

Your $i obvious has the following
characteristics:

a) The delimiter is character (>)

b) You want the second field

c) And finally, from the second field
remove all instances of characters
comma (,) and single-quote (').

Here is a little Perl script to
demonstrate what you want:

#!/usr/bin/perl
my $LINE = "baba > second row contains comma (,) and quote (') >third something >fourth and so on>fifth";

my @ARR = split(/>/, $LINE);

print "BEFORE @ARR[1]\n";

@ARR[1] =~ s/'//g;
@ARR[1] =~ s/,//g;

print "AFTER @ARR[1]\n";
exit;

When you run it:

BEFORE second row contains comma (,) and quote (')
AFTER second row contains comma () and quote ()

Perl is very powerful and I am sure there
will be other elegant solutions in this
Forum.

Cheers,

VK2COT
VK2COT - Dusan Baljevic
H.Merijn Brand (procura
Honored Contributor

Re: How to split in Perl??

Note that perl is not allways the best tool for simple things like this

$ echo "a>b>c>3" | perl -aF\> -ple'($_=$F[1])=~y/\47"//'
b

or in a script

(my $f2 = split m/>/, $i, -1) =~ tr/'"//d;

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Rajesh SB
Esteemed Contributor

Re: How to split in Perl??

Hello Guys,

I used following commands to extract the interested values, instead of using array.

for($i=0;$i<=$#fstab_array;$i++)
{
($fstype0, $fstype) = split(/>/, "$fstab_array[$i]");
$fstype =~ tr/',//d;
$i += 1;
}



Thanks & Regards,
Rajesh