1

I would like to execute some basic mathematical calculations in batch/Cygwin, but the solution, described in StackOverflow question: Calculating the sum of two variables in a batch script is using the set /A command for that matter.

This does not suit me, because I'd like to have everything in a pipe (UNIX style, hence the Cygwin).

My idea is the following: I have a list of files, containing an entry. I'd like to show, for all mentioned file, the one line behind that entry.

Therefore I was thinking about following approach:

Find the line where the entry is found: fgrep -n <entry> // this shows the line number together with the entry itself
Only show the line number: fgrep -n <entry> | awk -F ':' '{print $1}'
Add '1' to that number
Take the first amount of entries : head -<new number>
Only take the last line : tail -1

But as I don't know how to add 1 to a number, I am stuck here.

I've already tried using bc (basic calculator) but my Cygwin installation seems not to cover that. As I want to have everything within one piped line, the usage of set /A has not sence.

Does anybody have an idea?
Thanks in advance

3
  • 1
    You can add BC to your cygwin installation by re-running setup and installing it. Commented Feb 10, 2017 at 13:11
  • The cygwin package is named "bc": cygwin.com/cgi-bin2/… Commented Feb 10, 2017 at 14:17
  • expr can be used to do some arithmetic Commented Feb 10, 2017 at 20:33

2 Answers 2

1

Sorry, sorry, I just realised that awk is capable of doing some basic calculations, so by replacing {print $1} by {print $1 + 1} my problem is solved.

Sign up to request clarification or add additional context in comments.

Comments

0

Bash has builtin support for declaring variables to be integer and for doing arithmetic on integers. Bash has a help command and you should have both man bash and info bash installed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.