如何使用竖线字符分隔符分隔字段

编程入门 行业动态 更新时间:2024-10-25 02:24:43
本文介绍了如何使用竖线字符分隔符分隔字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

如何使用竖线字符分隔符分隔字段

本文发布于:2023-10-16 10:24:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:竖线   字段   如何使用   字符   分隔符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!