Pipe symbol | in AWK field delimiter

前端 未结 3 1577
南旧
南旧 2020-12-20 18:15

I have a file foo that has the following data:

A<|>B<|>C<|>D
1<|>2<|>3<|>4

I want to prope

3条回答
  •  春和景丽
    2020-12-20 18:31

    The pipe is a special character in a regex, so you need to escape it with a backslash. But this backslash is also a special character for the string literal, so it needs to be escaped again. So you end up with the following:

    awk -F '<\\|>' '{$1=$1}1'
    
    awk 'BEGIN {FS="<\\|>"} {$1=$1}1' 
    

    The reason for this syntax is explained quite well here: http://www.gnu.org/software/gawk/manual/gawk.html#Computed-Regexps. In short, the expression is parsed twice.

提交回复
热议问题