我知道这个问题已经被提出,但是我发现没有一种解决方案对我有用!我有一个程序,其输出是这样的:
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|lpsbash某处是否有bug?令我惊讶的是,在我的Linux主机和使用busybox的ash的嵌入式设备上,结果都是一样的!任何指针都将不胜感激!
编辑 我的错,输出在stderr ... ._.
./spawn 1 2>&1 | cut -d '|' -f2 4615对不起,很抱歉!
解决方案只需重复我在评论中猜测的内容作为答案,现在提问者已经确认这是问题所在.
这里的问题是./spawn 1正在输出到标准错误,而不是标准输出.您可以使用2>&1重定向输出,因此以下操作应该有效:
./spawn 1 2>&1 | cut -d '|' -f2I 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|lpsI'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|lpsIs 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 4615Sorry 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
更多推荐
如何使用竖线字符分隔符分隔字段
发布评论