如何使用竖线字符分隔符分隔字段 [英] How to separate fields with pipe character delimiter

查看:2195
本文介绍了如何使用竖线字符分隔符分隔字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被提出,但是我发现没有一种解决方案对我有用!我有一个程序,其输出是这样的:

COUNT|293|1|lps

我对拥有第二个领域感兴趣,但是这些尝试都没有奏效:

./spawn 1 | cut -d '|' -f2
./spawn 1 | cut -d \| -f2
./spawn 1 | awk -F "|" '{print $2}'
./spawn 1 | awk 'BEGIN{FS="|"} {print $2}'
./spawn 1 | sed 's/|/;/g'
./spawn 1 | sed 's/\|/;/g'

但是输出总是相同的:

COUNT|293|1|lps

bash某处是否有bug?令我惊讶的是,在我的Linux主机和使用busybox的ash的嵌入式设备上,结果都是一样的!任何指针都将不胜感激!

编辑 我的错,输出在stderr ... ._.

./spawn 1 2>&1 | cut -d '|' -f2
4615

对不起,很抱歉!

解决方案

只需重复我在评论中猜测的内容作为答案,现在提问者已经确认这是问题所在.

这里的问题是./spawn 1正在输出到标准错误,而不是标准输出.您可以使用2>&1重定向输出,因此以下操作应该有效:

./spawn 1 2>&1 | cut -d '|' -f2

I know this question has already been asked but no of the solution I've found worked for me! I have a program that has an output like this:

COUNT|293|1|lps

I'm interested in having the second field however no one of these tries worked:

./spawn 1 | cut -d '|' -f2
./spawn 1 | cut -d \| -f2
./spawn 1 | awk -F "|" '{print $2}'
./spawn 1 | awk 'BEGIN{FS="|"} {print $2}'
./spawn 1 | sed 's/|/;/g'
./spawn 1 | sed 's/\|/;/g'

But the output is always the same:

COUNT|293|1|lps

Is there a bug somewhere in bash? I would be surprised, the results is the same on my Linux host and on my embedded device using busybox's ash! Any pointer is strongly appreciated!

EDIT My fault, the output was in stderr ... ._.

./spawn 1 2>&1 | cut -d '|' -f2
4615

Sorry for ennoying!

解决方案

Just repeating what I guessed in a comment as an answer, now that the questioner has confirmed that this is the problem.

The problem here is that ./spawn 1 is outputting to standard error, not standard output. You can redirect the output using 2>&1, so the following should work:

./spawn 1 2>&1 | cut -d '|' -f2

这篇关于如何使用竖线字符分隔符分隔字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆