Search code examples
perlcommand-linerecord

perl -a: How to change column separator?


I want to read the columns in a file where the separator is :.

I tried it like this (because according to http://www.asciitable.com, the octal representation of the colon is 072):

$ echo "a:b:c"  | perl -a -072 -ne 'print "$F[1]\n";' 

I want it to print b, but it doesn't work.


Solution

  • Look at -F in perlrun:

    % echo "a:b:c" | perl -a -F: -ne 'print "$F[1]\n";'
    b
    

    Note that the value is taken as regular expression, so some delimiters may need some extra escaping:

    % echo "a.b.c" | perl -a -F. -ne 'print "$F[1]\n";'
    
    % echo "a.b.c" | perl -a -F\\. -ne 'print "$F[1]\n";'
    b