How to separate fields with pipe character delimiter

社会主义新天地 提交于 2020-01-24 04:13:05

问题


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!


回答1:


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



回答2:


$ echo 'COUNT|293|1|lps' | cut -d'|' -f2
293

It works here.



来源:https://stackoverflow.com/questions/9018691/how-to-separate-fields-with-pipe-character-delimiter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!