我的文件看起来像这样
abc ||| xyz ||| foo bar
hello world ||| spam ham jam ||| blah blah
我想提取一个特定的列,例如我可以做到的:
sed 's/\s|||\s/\\t/g' file | cut -f1
但是还有其他方法吗?
最佳答案
Since
|
is a valid regex expression, it need to be escaped\\|
or put in square brackets[|]
你可以这样做:
awk -F' \\|\\|\\| ' '{print $1}' file
其他一些可行的变体
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
示例:表达式中的每个
\
4个\
,因此总共12个\
。awk -F' \\\\\\\\\\\\ ' '{print $2}' file
xyz
要么
awk -F' \\\\{3} ' '{print $2}' file
xyz
或这个,但并不简单
awk -F' [\\\\]{3} ' '{print $2}' file
xyz
awk -F' [\\\\][\\\\][\\\\] ' '{print $2}' file
xyz
关于sed - 如何使用带有多个字符分隔符的cut? Unix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25447324/