Any reliable way to sort file columns?
I have a Perl script using a call system
to sort a tsv file:
system("sort -k8 -r -n -t \$'\t' in.txt > out.txt");
It works great on CentOS and SUSE Linux. But on Ubuntu this gives an error:
sort: multi-character tab `$\t'
There seems to be a problem with another OS interpreting the quote differently. Do you have a simpler but more reliable way to sort a tsv file in Perl?
source to share
Typically, you should use an array call system
to avoid the shell, but you have I / O redirection in the command that is difficult to deal with. OTOH, sort
allows you to specify an output file from -o
, and the named file can be one of the inputs (although it won't be here):
my @cmd = ( "sort", "-k8", "-rn", "-t", "\t", "-o", "out.txt", "in.txt" );
system(@cmd);
The shell is not invoked; the tab is not distorted. I have combined options -r
and -n
into one; you can leave them separate if you like, or (as a last resort) add them after the option -k8
.
source to share