Perl -a: How to change the column delimiter?

I want to read the columns in a file where the delimiter 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 does not work.

+6
source share
2 answers

See -F in perlrun:

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

Note that this value is accepted as a regular expression, so some delimiters may require additional escaping:

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

-0 specifies the separator of the record (string). This was the reason that Perl would get three lines:

 >echo a:b:c | perl -072 -nE"say" a: b: c 

Since there are no spaces on any of these lines, $F[1] will be empty if you need to use -a .

-F indicates the input field separator. Is this what you want.

 perl -F: -lanE'say $F[1];' 

Or if you are stuck with old Perl:

 perl -F: -lane'print $F[1];' 

Command line options are documented in perlrun .

+5
source

Source: https://habr.com/ru/post/896339/


All Articles