Calculating the sum of two variables in a batch/UNIX single piped line
NickName:Dominique Ask DateTime:2017-02-10T21:08:37

Calculating the sum of two variables in a batch/UNIX single piped line

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

Copyright Notice:Content Author:「Dominique」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42160201/calculating-the-sum-of-two-variables-in-a-batch-unix-single-piped-line

Answers
Dominique 2017-02-10T13:10:53

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.",


More about “Calculating the sum of two variables in a batch/UNIX single piped line” related questions

Calculating the sum of two variables in a batch/UNIX single piped line

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 ...

Show Detail

Calculating the sum of two variables in a batch script

This is my first time on Stack Overflow so please be lenient with this question. I have been experimenting with programming with batch and using DOSbox to run them on my linux machine. Here is th...

Show Detail

Two variables on the same line for a calculator batch file

I am currently making a batch file to multiply two numbers. Here is my code: @echo off cls echo Math Commands set /p num1= &amp; set /p num2= set /a sum1="num1 * num2" cls echo Your question is %n...

Show Detail

A batch script that stores optional piped multiline string and optional arguments

Consider the cmd/batch script program.bat which accepts optional piping and command-line arguments. The script will be executed in four different ways: program.bat: without piping and arguments pr...

Show Detail

Sum/Average numbers in a single line - UNIX

I'm working on a small script to take 3 numbers in a single line, sum and average them, and print the result at the end of the line. I know how to use the paste command, but everything I'm finding is

Show Detail

passing on piped data in batch file

The situation I'm sitting at the receiving end of program A (redmon, a port monitor). I need to write a batch script that program A can call to pipe a large amount of data (PostScript) into. When...

Show Detail

calculating the sum of nodes in a single verticle line of a binary tree

For a binary tree i want to get the sum of all nodes that fall in a single verticle line. I want the sum of nodes in each verticle node A / \ B C ...

Show Detail

calculating the sum of nodes in a single verticle line of a binary tree

For a binary tree i want to get the sum of all nodes that fall in a single verticle line. I want the sum of nodes in each verticle node A / \ B C ...

Show Detail

Windows Batch Echoing Two Variables On One Line

I am trying to make a batch file tool at my work to assist with troubleshooting. I am trying to print two variables on one line, but everything I have tried does not work. How can I accomplish ec...

Show Detail

Capturing true STDIN piped to a batch file

I want to access STDIN from inside a batch file after some other commands. I know that the first command in a .BAT file receives STDIN but I want to first run some other commands and then capture S...

Show Detail