How to store value from cut command into a Perl array? -


my @up = `cat abc.txt|head -2|tail -1|cut -d' ' -f1-3`; 

instead of storing individual fields in array. it's storing entire output string in first element.

this output getting

$up[0] = 'xxx 12 234' 

i want this

@up = ('xxx', 12, 234) 

|

it looks want first 3 space-delimited fields of second line of file abc.txt

the problem backticks return 1 line of output in each element of array, , because cut prints 3 fields on single line, appear single array element.

you split value again inside perl, when have whole of perl language available, it's wasteful use shell simple , should in perl

this program ask. i've used data::dump can verify contents of @up wanted

use strict; use warnings 'all';  use data::dump;  @up = {     open $fh, '<', 'abc.txt' or die $!;     <$fh>;  # skip 1 line     (split ' ', <$fh>)[0 .. 2]; };  dd \@up; 

output

["xxx", 12, 234] 

Comments