In the Clash of Code when trying convert dec to bin numbers, this code works on my ubuntu
read;sed ‘s/^/obase=2;/’|bc
but in CG tests it throws
(standard_in) 1: syntax error
is it a CG bug or is it just me?
I can’t see how your command could work at all. Your read doesn’t store anything, only return an exit code that you are ignoring anyway.
Using a read, the following will work in interactive mode.
$ read line; echo $line | sed 's/^/obase=2;/' | bc
In a Clash of Code, you probably* want something more like:
$ cat | sed 's/^/obase=2;/' | bc
(* I never played one, so I can’t say for sure.)
The read command just ignores the first line where the number of lines is stored.
Why should I use cat? The sed reads from stdin anyway… or is there a catch?
You’re right for the cat. That was an entry for the UUOC and we can all forget it. If your first read was just there to discard a line, the rest of your command is correct. However, the error message is clearly related to bc, so I guess you are not feeding it with a valid string. Printing the output of sed could help here:
sed 's/^/obase=2;/' | tee /dev/stderr | bc
It’s probably because the last input line is not properly terminated by a newline (\n).
$ printf '3\n1\n2\n3'| (read;sed 's/^/obase=2;/'|bc)
1
10
(standard_in) 1: syntax error