codyLine
codyLine

Reputation: 519

find first number in each line, do some arithmetic and replace it

I have hundreds of lines like this one:
INSERT INTO table1 VALUES (5566, 1979, 'Montag', '06:00', '19:00', 1, 'ON', NULL
I need to add to the first number 5566 another number, let say 50 and this in each line.
There is many ways how to get the first number with sed or awk but how can I perform the arithmetic and replace the number?
Any hints?

Upvotes: 1

Views: 91

Answers (1)

anubhava
anubhava

Reputation: 785611

You can do use this awk:

s='INSERT INTO table1 VALUES (5566, 1979, 'Montag', '06:00', '19:00', 1, 'ON', NULL'
echo "$s"|awk '{sub(/[^0-9]+/, "", $5); $5+=50; $5="(" $5 ","} 1'
INSERT INTO table1 VALUES (5616, 1979, Montag, 06:00, 19:00, 1, ON, NULL

Upvotes: 2

Related Questions