'How to perform arithmetic on every row of a file via piping [duplicate]
Lets say we have a simple file data.txt which has the following content:
1
2
3
Is there a command to perform arithmetic on every row value of the file via piping? I'm looking for something like cat data.txt | arit "+10" which would output:
11
12
13
The best thing I know is performing arithmetic by using bc for individual values, for example echo "1+10" | bc, but I wasn't able to apply this to my own example which contains many values with trailing newlines.
Solution 1:[1]
You could use awk:
awk '{print $1+10}' data.txt
Solution 2:[2]
My first impulse is to use sed. (Hey, it's the tool I always reach for.)
cat data.txt | sed 's/$/+10/' | bc
EDIT: or like so
sed 's/$/+10/' data.txt | bc
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Kugelman |
| Solution 2 |
