Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

awk commandline calculator unix/win

81 views
Skip to first unread message

Mike Sanders

unread,
May 4, 2016, 10:58:52 AM5/4/16
to
From the article 'Create an Awesome Command Line Calculator'...

'To create the calculator, you can just enter this function on the command
line to test out how it works, or add it into your ~/.bashrc file to make
sure that it's always available after restarting the terminal.

calc(){ awk "BEGIN{ print $* }" ;}

To use it, you can simply type calc followed by the calculation you want to
solve, making sure to use quotes around the calculation if there are spaces
or special characters the shell can't handle. For instance, if you entered
calc 3*99/7+18 at the prompt, you'd get 60.4286 as the answer. For more
complex calculations, you can use parenthesis, but make sure to put quotes
around it, like this:

calc "((3+(2^3)) * 34^2 / 9)-75.89"'

article here:

<http://lifehacker.com/5396183/create-an-awesome-command-line-calculator>


A windows equivalent via s doskey macro: doskey calc=gawk "BEGIN{print $*}"


calc 5^3

125


notes...

with doskey '$*' is meta variable that passes all doskey arguments (only
doskey, bat/cmd scripts use '%*' instead).

--
Mike Sanders
www.peanut-software.com

Andrew Schorr

unread,
May 4, 2016, 9:10:03 PM5/4/16
to
On Wednesday, May 4, 2016 at 10:58:52 AM UTC-4, Mike Sanders wrote:
> calc(){ awk "BEGIN{ print $* }" ;}

Cute, but is it better than this?

calc() { echo "$@" | bc -l; }

Regards,
Andy

Kenny McCormack

unread,
May 5, 2016, 3:20:44 AM5/5/16
to
In article <04062708-efc3-42a6...@googlegroups.com>,
Or this:

alias c "echo '\!*' | sed 's/:/;/g' | bc -l"

(Which I have had in my .tcshrc file since forever...)

--
When people wish to comment on something on which they have personal
knowledge, but do not wish to convey the fact that it is personal
knowledge, they often qualify their statement with "it's my
understanding". For instance, it's my understanding that when some
women are depressed they sit on the futon in a Snuggie, watch Lifetime,
and eat a whole tub of Rocky Road ice cream.

Mike Sanders

unread,
May 5, 2016, 6:11:09 AM5/5/16
to
Andrew Schorr <asc...@telemetry-investments.com> wrote:

> calc() { echo "$@" | bc -l; }

Spot on Andy.

--
Mike Sanders
www.peanut-software.com

Luuk

unread,
May 5, 2016, 7:11:38 AM5/5/16
to
On 05-05-16 09:20, Kenny McCormack wrote:
> In article <04062708-efc3-42a6...@googlegroups.com>,
> Andrew Schorr <asc...@telemetry-investments.com> wrote:
>> On Wednesday, May 4, 2016 at 10:58:52 AM UTC-4, Mike Sanders wrote:
>>> calc(){ awk "BEGIN{ print $* }" ;}
>>
>> Cute, but is it better than this?
>>
>> calc() { echo "$@" | bc -l; }
>>
>> Regards,
>> Andy
>
> Or this:
>
> alias c "echo '\!*' | sed 's/:/;/g' | bc -l"
>
> (Which I have had in my .tcshrc file since forever...)
>

Why is sed in there?

opensuse /home/luuk> tcsh --version
tcsh 6.18.01 (Astron) 2012-02-14 (x86_64-unknown-linux) options
wide,nls,lf,dl,al,kan,sm,color,filec
opensuse /home/luuk> c 2+3
5
opensuse /home/luuk> c 2+3 : 4+5
5
9
opensuse /home/luuk> c 2+3 ; 4+5
5
4+5: Command not found.
opensuse /home/luuk>

Luuk

unread,
May 5, 2016, 7:13:37 AM5/5/16
to
hmm, i need (more) coffee ... ;)

the answer is too obvious...


Kenny McCormack

unread,
May 5, 2016, 8:12:23 AM5/5/16
to
In article <572b2a5a$0$5808$e4fe...@news.xs4all.nl>,
Luuk <lu...@invalid.lan> wrote:
...
>> Or this:
>>
>> alias c "echo '\!*' | sed 's/:/;/g' | bc -l"
>>
>> (Which I have had in my .tcshrc file since forever...)
>>
>
>Why is sed in there?

You've answered your own question. It is so that you can do multiple
expressions in a single command line without having to worry about quoting.

--
"Unattended children will be given an espresso and a free kitten."

Aharon Robbins

unread,
May 6, 2016, 6:39:22 AM5/6/16
to
OK Andy, that's it. A non-awk solution?!?! I'm revoking your
push rights to the git repo.

(Joke, of course.)
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com

Luuk

unread,
May 6, 2016, 4:06:26 PM5/6/16
to
On 06-05-16 12:35, Aharon Robbins wrote:
> In article <04062708-efc3-42a6...@googlegroups.com>,
> Andrew Schorr <asc...@telemetry-investments.com> wrote:
>> On Wednesday, May 4, 2016 at 10:58:52 AM UTC-4, Mike Sanders wrote:
>>> calc(){ awk "BEGIN{ print $* }" ;}
>>
>> Cute, but is it better than this?
>>
>> calc() { echo "$@" | bc -l; }
>>
>> Regards,
>> Andy
>
> OK Andy, that's it. A non-awk solution?!?! I'm revoking your
> push rights to the git repo.
>
> (Joke, of course.)
>

+1

and, of course, the Windows solution was not given despite the fact that
the OP did give it.....

and arent we all using Windows?

;)

Andrew Schorr

unread,
May 6, 2016, 7:21:42 PM5/6/16
to
On Friday, May 6, 2016 at 6:39:22 AM UTC-4, Aharon Robbins wrote:
> OK Andy, that's it. A non-awk solution?!?! I'm revoking your
> push rights to the git repo.

Had he used "gawk -M", I would not have objected. :-)

Chris F.A. Johnson

unread,
May 7, 2016, 1:08:04 AM5/7/16
to
On 2016-05-04, Mike Sanders wrote:
> From the article 'Create an Awesome Command Line Calculator'...
>
> 'To create the calculator, you can just enter this function on the command
> line to test out how it works, or add it into your ~/.bashrc file to make
> sure that it's always available after restarting the terminal.
>
> calc(){ awk "BEGIN{ print $* }" ;}

For years I used this:

calc()
{
awk 'BEGIN {print '"${*//x/*}"'}'
}

This allowed me to use x instad of *, which would have to be escaped.

I recently switched to this:

calc ()
{
echo "${*//x/*}" | bc -l
}


--
Chris F.A. Johnson
0 new messages