in reply to A problem with using the split command
This thread helped me with a problem I was having. Thanks!
Here is the command line test script that I wrote to test out some options. It allows you to see what response occurs in your system.
This was tested under Xubuntu linux with a Terminator terminal. For my system I found a DIFFERENCE in behavior between a loaded test file with pipe symbol used as the separator and the pipe symbol being used in a test data line entered from the command line.
You can also experiment with other potential separators from the command line, but you will have to edit the script and a sample data file to experiment with various separators loaded from a file on disk. A sample |(pipe) separated test file follows the script.
save program as split-test.pl
#!/usr/bin/perl -w use strict; # no warnings 'uninitialized'; use English; use utf8; binmode(STDOUT, ":utf8"); print "Using | separated data file loaded from disk;\n | IS escaped in + split\n"; open(IN,"< testdata.csv") || die "cannot open testdata.csv"; my @data = <IN>; chomp(@data); foreach my $line(@data){ foreach my $pwd (split(/\|/, "$line")) { print $pwd,"\n"; }; }; print "hit any key to continue: "; my $ans = <STDIN>; print "Using | separated data file from disk; | NOT escaped in split\n"; foreach my $line(@data){ foreach my $pwd (split(/|/, "$line")) { print $pwd,"\n"; }; }; print "hit any key to continue: "; $ans = <STDIN>; print "following examples use | separated test string in script in \n different ways\n"; print"default separator | NOT escaped in default test string\n"; foreach my $pwd (split(/\|/, "| *foo | *foo1 | *foo2")) { print $pwd,"\n"; }; print"default separator | IS escaped in default test string\n"; foreach my $pwd (split(/\|/, "\| *foo \| *foo1 \| *foo2")) { print $pwd,"\n"; }; print "this section is a loop that allows you to try different separat +ors that you enter from command line\n"; print "you can experiment by using a character alone or proceeding the + character with an \ as an escape'\n for example, using | and then using \ |/.\n"; while(-1){ print "start of loop enter NEW separator: "; my $sep = <STDIN>; chomp($sep); print "separator IS escaped in split command:\n"; print "separator $sep is NOT escaped in split command:\n"; foreach my $pwd (split(/\\$sep/, "$sep *foo $sep *foo1 $sep *foo2")) { print $pwd,"\n"; }; print"\n\t ****\n\n"; print "test separator ($sep) on command line testline\n"; print "using $sep: paste testline here: "; my $testline =<STDIN>; chomp($testline); print "separator IS escaped in split command:\n"; my @testarray = split(/\\$sep/,$testline) ; foreach my $item(@testarray){ print "$item\n"; }; print "separator is NOT escaped in split command:\n"; @testarray = split(/"$sep"/,$testline) ; foreach my $item(@testarray){ print "$item\n"; }; }#endwhile
sample test file - save as "testdata.csv
| *foo | *foo1 | *foo2 | *foo4| *foo5 | *foo6
|
|---|