Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change strings incrementally using sed or awk

Tags:

bash

sed

awk

I've a file over 2k lines which looks like:

pl (IdPl,X,Y,TitleLine,Name,Description,idRowMetric,idPlMetric,Person) Values (26916,1000,3750,'','','11',null,NULL,1)
pla (IdPl,EncLevel,IdSection) Values (26916,2,21)
pla(IdPl,EncLevel,IdSection) Values (26916,1,1065)

Pl (IdPl,X,Y,TitleLine,Name,Description,idRowMetric,idPlMetric,Person) Values (26917,2900,3750,'','','11',null,NULL,1)
Pla(IdPl,EncLevel,IdSection) Values (26917,2,21)
Pla (IdPl,EncLevel,IdSection) Values (26917,1,1065)

Pl (IdPl,X,Y,TitleLine,Name,Description,idRowMetric,idPlMetric,Person) Values (26918,1050,3750,'','','11',null,NULL,1)
Pla (IdPl,EncLevel,IdSection) Values (26918,2,21)
Pla (IdPl,EncLevel,IdSection) Values (26918,1,1065)

Pl (IdPl,X,Y,TitleLine,Name,Description,idRowMetric,idPlMetric,Person) Values (26919,1100,3750,'','','11',null,NULL,1)
Pla (IdPl,EncLevel,IdSection) Values (26919,2,21)
Pla (IdPl,EncLevel,IdSection) Values (26919,1,1065)

How do I replace all 26916 on -1, 26917 on -2 and etc using sed or awk?

like image 200
kate Avatar asked Feb 28 '26 14:02

kate


1 Answers

Using sed:

sed -r 's/(.*)([0-9]{5})(.*)/echo "\1$((-(\2-26915)))\3"/ge' file

like image 78
Julien Lopez Avatar answered Mar 03 '26 05:03

Julien Lopez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!