How to split channel delimited fields

I know this question has already been asked, but no solution I found worked for me! I have a program that has this output:

COUNT|293|1|lps 

I am interested in having a second field, but none of these attempts 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 conclusion is always the same:

 COUNT|293|1|lps 

Is there an error somewhere in bash? I would be surprised the results would be the same on my Linux host and on my embedded device using the busybox! Any pointer is much appreciated!

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

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

Sorry for ennoying!

+4
source share
2 answers

Repeating what I guessed in the comment as the answer, now that the questionnaire has confirmed that this is a problem.

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

 ./spawn 1 2>&1 | cut -d '|' -f2 
+7
source
 $ echo 'COUNT|293|1|lps' | cut -d'|' -f2 293 

He works here.

+2
source

Source: https://habr.com/ru/post/1393164/


All Articles