How to use cut with multiple character delimiter? unix

六眼飞鱼酱① 提交于 2019-11-30 06:33:31

Since | is a valid regex expression, it need to be escaped \\| or put in square brackets [|]

You can do this:

awk -F' \\|\\|\\| ' '{print $1}' file

Some other variation that work as well

awk -F' [|][|][|] ' '{print "$1"}' file
awk -F' [|]{3} ' '{print "$1"}' file
awk -F' \\|{3} ' '{print "$1"}' file
awk -F' \\|+ ' '{print "$1"}' file
awk -F' [|]+ ' '{print "$1"}' file

\ as separator does not work well in square brackets, only escaping, and many escape :)

cat file
abc \\\ xyz \\\ foo bar

Example: 4 \ for every \ in the expression, so 12 \ totalt.

awk -F' \\\\\\\\\\\\ ' '{print $2}' file
xyz

or

awk -F' \\\\{3} ' '{print $2}' file
xyz

or this but not much simpler

awk -F' [\\\\]{3} ' '{print $2}' file
xyz

awk -F' [\\\\][\\\\][\\\\] ' '{print $2}' file
xyz

You can use awk to do it -

$ awk 'BEGIN {FS=" \|\|\| ";}{print $1}' file

Replace $1 with $2, $3, etc.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!