You would need to modify this for the formatting you want (I split your data on whitespace, manipulate it, and then join it again with simple tabs in between), but I believe the following code is one way to do what you are asking.
#!/usr/bin/perl use warnings; use strict; open(my $FILE, '<', 'input_file.txt') or die $!; my %output_files; my $line_number = 1; while (<$FILE>) { chomp; my @columns = split; my $value = abs($columns[$#columns]); if(!exists $output_files{$value}) { open($output_files{$value}, '>', "test-out-$value.txt") or die + $!; } my $fh = $output_files{$value}; splice @columns, 0, 0, $columns[$#columns], $line_number; pop @columns; my $output_line = join "\t", @columns; print $fh "$output_line\n"; $line_number++; }
EDIT: Didn't notice you moved the last column to be the first column right away, updated code for that.
In reply to Re: divide multi-column input file into sub-files depending on specific column's value
by perldigious
in thread divide multi-column input file into sub-files depending on specific column's value
by angela2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |