1

I'm using grep on Ubuntu Desktop 20.04.

I want to get player movements from gnuchess game by using grep each turn. The first command i used is this :

gnuchess -mq | grep -a "Black ([0-9][0-9]*) : [0-9][0-9]*. [a-z][0-9]" | grep "[a-z][0-9]"

for the first grep, if movements are 'e3' and 'e5', the output is :

Black (1) : 1. e5

I expect the second grep give me e5 but it doesn't. Output is blank.

Yet when i redirect gnuchess output to a file and use the same command it works and shows e5. Why?

Edit:

The problem was with output buffering. Added my answer below.

  • 1
    @K7AAY Edited my post. I found the problem and wrote my answer but can't verify it for 2 days. – ariaman5 May 15 '20 at 17:02
2

turning off output buffering did the work.

gnuchess -mq | stdbuf -o0 grep -a "Black ([0-9][0-9]*) : [0-9][0-9]*. [a-z][0-9]" | grep -o "[a-z][0-9]"

For more information: this

-1

grep returns the whole line where it finds a match, so the behaviour you experience is the expected one.

If you are sure that what you want is the fifth token of the string, you can use awk:

gnuchess -mq | grep -a "Black ([0-9][0-9]*) : [0-9][0-9]*. [a-z][0-9]" | awk '{print $5}'

For example: echo 'Black (1) : 1. e5' | grep -a "Black ([0-9][0-9]*) : [0-9][0-9]*. [a-z][0-9]" | awk '{print $5}' returns only e5.

  • 1
    Still it doesn't work. Your example works but on gnuchess it doesn't. If i remove awk, output is 'Black (1) : 1. e5' though. – ariaman5 May 15 '20 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.