Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to replace a number greater than a specified number at a specified position

Tags:

bash

shell

sed

I need to write a script to replace all the numbers greater than an specified number which is in following position.

1499011200 310961583 142550756 313415036 146983209

Here I am writing a script if the second term exceeds in value greater than 300000000. I need the whole line to be replaced by my desired value like

1499011200 250000000 XXXX XXXX XXXX

I hope I have made my question clear.

Thanks in advance

like image 385
srt Avatar asked Dec 13 '25 05:12

srt


1 Answers

This might work for you (GNU sed):

sed -r '/^\S+\s+(300000000|[1-2][0-9]{8}|[0-9]{1,8})\s/!c change' file

If it's 300000000 or less keep it, otherwise change it.

Or using substitution:

sed '/^\S\+\s\+\(300000000\|[1-2][0-9]\{8\}\|[0-9]\{1,8\}\)\s/!s/^\(\S\+\s\+\).*/\1250000000 XXXX XXXX XXXX/' file
like image 191
potong Avatar answered Dec 14 '25 20:12

potong



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!