8

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!

4

2 回答 2

9

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

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

./spawn 1 2>&1 | cut -d '|' -f2
于 2012-01-26T13:43:18.793 回答
2
$ echo 'COUNT|293|1|lps' | cut -d'|' -f2
293

它在这里工作。

于 2012-01-26T13:24:25.427 回答