0

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.

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
Moritz Loritz
  • 372
  • 2
  • 13

2 Answers2

2

You could use awk:

awk '{print $1+10}' data.txt
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
1

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
Beta
  • 92,251
  • 12
  • 140
  • 145