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
  • 349,597
  • 67
  • 533
  • 578
Moritz Wolff
  • 436
  • 1
  • 7
  • 16

2 Answers2

2

You could use awk:

awk '{print $1+10}' data.txt
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
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
  • 96,650
  • 16
  • 149
  • 150