my $line = \"file1.gz file2.gz file3.gz\";
my @abc = split(\'\', $line);
print \"@abc\\n\";
Expected output:
file1.gz
file2.gz
file3.gz
Splitting a string by whitespace is very simple:
print $_, "\n" for split ' ', 'file1.gz file1.gz file3.gz';
This is a special form of split actually (as this function usually takes patterns instead of strings):
As another special case,
splitemulates the default behavior of the command line toolawkwhen thePATTERNis either omitted or a literal string composed of a single space character (such as' 'or"\x20"). In this case, any leading whitespace inEXPRis removed before splitting occurs, and thePATTERNis instead treated as if it were/\s+/; in particular, this means that any contiguous whitespace (not just a single space character) is used as a separator.
Here's an answer for the original question (with a simple string without any whitespace):
Perhaps you want to split on .gz extension:
my $line = "file1.gzfile1.gzfile3.gz";
my @abc = split /(?<=\.gz)/, $line;
print $_, "\n" for @abc;
Here I used (?<=...) construct, which is look-behind assertion, basically making split at each point in the line preceded by .gz substring.
If you work with the fixed set of extensions, you can extend the pattern to include them all:
my $line = "file1.gzfile2.txtfile2.gzfile3.xls";
my @exts = ('txt', 'xls', 'gz');
my $patt = join '|', map { '(?<=\.' . $_ . ')' } @exts;
my @abc = split /$patt/, $line;
print $_, "\n" for @abc;