Syntax error piping to bc on command line - works when assigned to var


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error piping to bc on command line - works when assigned to var
# 1  
Old 01-09-2012
Syntax error piping to bc on command line - works when assigned to var

I have a script which outputs some timing data a line at a time. There are approx. 10 lines echoed, each line looks something like this:
Code:
0.741 http://checkip.dyndns.org 94.170.119.226

Since I needed to add all the values in the first column, I piped the output to grep, matching and printing the first numerical column, then piped it on to tr to replace new lines with '+', then piped to sed to replace the final '+' with nothing, and finally to bc to add the values. Then bc gave me a stdin syntax error - damn it, maybe I'm not quite so clever as I thought I was. Smilie

On examining the code I discovered that if I left off the final pipe to bc I got what I thought I should have - that being:
Code:
$ getip test | grep -o "^[0-9.]*" | tr "\n\r\f" "+" | sed "s/\(.*\)+/\1/" 
0.409+0.087+0.116+0.294+0.214+0.342+0.595+0.232+0.380+0.494 [Note: no new line, prompt is here as expected]

$ echo 0.409+0.087+0.116+0.294+0.214+0.342+0.595+0.232+0.380+0.494 | bc
3.163

So I copied and pasted those values into an echo statement, piped to bc, and it worked fine as shown above.

As another test I ran the command again, this time assigning the result to a variable, and then piped the variable to bc. That also worked fine, see code below (the top line has the error shown when piped to bc):
Code:
$ getip test | grep -o "^[0-9.]*" | tr "\n\r\f" "+" | sed "s/\(.*\)+/\1/" | bc
(standard_in) 1: syntax error

$ var1=$(getip test | grep -o "^[0-9.]*" | tr "\n\r\f" "+" | sed "s/\(.*\)+/\1/")

$ echo $var1
0.332+0.111+0.081+0.283+0.155+0.355+0.607+0.297+0.382+0.452+0.294

$ echo $var1 | bc
3.349

What I want to know is why I get the syntax error from bc when I run it as one line? Any ideas?

Many thanks.

PS. The somewhat simpler alternative works with no problems.
Code:
$ getip test | grep -o "^[0-9.]*" | awk '{sum+=$1} END {print sum}'
2.999

# 2  
Old 01-09-2012
I suspect it has something to do with extra spaces being stripped out by the echo.

This'd be far simpler to do with awk.

Code:
getip | awk '{ T+=$1 } END { print T }'

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-09-2012
FWIW - awk was made precisley for what you are doing
Code:
getip test | awk '{sum += $1} END {print sum}'

By default awk does double precision arithmetic (floating point with 15 significant digits)
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 01-09-2012
Corona688 and jim mcnamara: Did neither of you see the PS in my post !! Smilie I know I failed to realize that $1 would give me the first column (I assumed that would be the whole line). It's not that I can't do it, just that I want to understand what I am doing wrong with the pipe to bc. Anyone know?

Corona688: There are no extra spaces, or any spaces at all come to think of it.

Thanks guys. Cheers.
# 5  
Old 01-09-2012
I always endeavour to answer the question, and the actual problem. Smilie Seeing your ps actually encouraged me to suggest more awk things.

It may be the complete lack of a newline that causes bc to fail, too. echo will add one on the end, but just piping it in would end up with nothing. Many commands are picky about lines without newlines -- some versions of awk and sed, for instance.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 01-09-2012
how about:
Code:
getip test | grep -o "^[0-9.]*" | tr "\n\r\f" "+" | sed 's/+$/&0/' | bc

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 01-09-2012
Explanation of the error.
Your "tr" command strips out all line terminators (\n) for some unknown reason and converts them to "+" signs. My version of "sed" will not process lines without a line terminator, but yours appears to work (to my surprise).

The "bc" program fails because the input stream is not terminated with a linefeed character (\n).

When you "echo" the variable $var1 the "echo" statement appends a linefeed character (\n).

Btw. The unix standard version of "tr" will not translate three different characters into one. The length of the translate list must be the same both sides.
Code:
echo "xyz" | tr 'xyz' '+'
+yz
echo "xyz" | tr 'xyz' '+++'
+++

In your script this would only matter if there were carriage-return or form-feed characters in the line (which there are clearly not).
This User Gave Thanks to methyl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command works at command line but not from cron

Oracle Linux 6. Trying to set up a simple monitoring of memory usage. This command does exactly what I want at the command line: echo $(date +%Y-%m-%d" "%H:%M:%S) $(grep PageTables /proc/meminfo) >> /home/oracle/meminfo.logBut when I put it in my crontab: * * * * * echo $(date +%Y-%m-%d"... (2 Replies)
Discussion started by: edstevens
2 Replies

2. Shell Programming and Scripting

Command assigned to a variable is failed or not having any data - error

Hi, My command is getting stuck while running it. observed that the grep command doesn't returned any data ($? was 1) and it failed. This command is assigned into the variable and used in other command as script progresses. To continue the script output, i have to press ^C twice and script... (2 Replies)
Discussion started by: abhii
2 Replies

3. Shell Programming and Scripting

TCL - quotes inside a string assigned to a var

I know that set line "This is a line." puts whats between " inside the var line. But How do I do the same for set line "This is a line "with quotations" inside the string." (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Works on command line but not in script

OSX 10.9 I am building a script that evaluates the difference between 2 files. Here is a command that does not work transparently. Running this command in Terminal yields great results; however when I put that line in a .sh script, I get the errors shown below. Am I doing something silly? ... (1 Reply)
Discussion started by: sudo
1 Replies

5. Shell Programming and Scripting

Function works, trigger causes syntax error, whats my fault??

Needing a hint. Creating that function called Meter my simple script works well. What I want now is to start the last four commented lines to include or trigger a reaction, if there are more than n lines in that .txt-file it shall display that message for example. But the interpreter says there is... (3 Replies)
Discussion started by: 1in10
3 Replies

6. Shell Programming and Scripting

SH script, variable built command fails, but works at command line

I am working with a sh script on a solaris 9 zone (sol 10 host) that grabs information to build the configuration command line. the variables Build64, SSLopt, CONFIGopt, and CC are populated in the script. the script includes CC=`which gcc` CONFIGopt=' --prefix=/ --exec-prefix=/usr... (8 Replies)
Discussion started by: oly_r
8 Replies

7. UNIX for Dummies Questions & Answers

Works on command line but not in script

Hey guys. Hopefully this is an easy one but having reference similar problems on the web I still can't fix it. I am doing a recursive find and replace from a script. Of course I could just run the damn thing from the command line but it's bugging me now and want to get it working. grep -rl... (4 Replies)
Discussion started by: anthonyjstewart
4 Replies

8. Shell Programming and Scripting

Command line not getting assigned in script.

I am running the following script and passing the following command line arguments : DBCheckSum_control CA_SITE CA_SITE Can someone please tell me, why my command line args are not being accepted and assigned to variable "TABLE_NAME'' and "ACTION_TYPE"?? usage() { print "Usage: ${0} {... (3 Replies)
Discussion started by: Veenak15
3 Replies

9. UNIX for Advanced & Expert Users

Piping Mail with Exim Only Works Locally

Whenever I send an email to my catchall alias via SSH (i.e., locally), Exim successfully pipes the e-mail to a PHP script, as I told it to do. However, when I try to send an e-mail to my catchall alias via my e-mail client (i.e., remotely), Exim won't pipe the e-mail. Any ideas? My exim.conf... (0 Replies)
Discussion started by: Zhay
0 Replies

10. Shell Programming and Scripting

KSH script: piping passes command-line arguments

Dear forum I have the following small script: #!/bin/ksh echo -e "abba-o" | awk -F '-' '{ print $2 }' | cut -b 1It needs to be ksh.. in bash I don't have this problem. If I run this on opensuse 10.2 I get this as output: e If I run this on suse enterprise 10 sp2 then I get this: o ... (1 Reply)
Discussion started by: gemtry
1 Replies
Login or Register to Ask a Question