user2451501
user2451501

Reputation: 31

replace the assigned number in increasing order

I have file containing following format. In the following file I want to replace every t=0.00000 with t=0.00000+50 like 50.00000 without changing the format of file. my file looks like:

MODULE  for the   Modulate t=   0.00000

MODULE  for the    Modulate t=   4.00000

MODULE   overshoot  Modulate t=   8.00000

MODULE   fail for  Modulate t=  12.00000

MODULE   successful for the Modulate t=  16.00000

I did something like this but not able to increment and its taking much time manually.

sed -i 's/MODULE  for the   Modulate t=   0.00000/MODULE  for the   Modulate t=50 /g'  file.txt

expected output:

MODULE  for the  Modulate t=   50.00000

MODULE  for the  Modulate t=  54.00000

MODULE  overshoot   Modulate t=  58.00000

MODULE   fail for  Modulate t=  62.00000

MODULE   successful for the Modulate t=  66.00000

Upvotes: 1

Views: 91

Answers (3)

Borodin
Borodin

Reputation: 126742

In a Perl one-line command

perl -pe 's/Modulate\s+t=\K\s*([0-9.]+)/sprintf '%10.5f',$1+50/e' my_file

output

MODULE  for the   Modulate t=  50.00000
MODULE  for the    Modulate t=  54.00000
MODULE   overshoot  Modulate t=  58.00000
MODULE   fail for  Modulate t=  62.00000
MODULE   successful for the Modulate t=  66.00000

Upvotes: 4

123
123

Reputation: 11216

Using awk

awk 'NF{$NF=sprintf("%.5f",$NF+50)}1' file

MODULE  for the  Modulate t=   50.00000

MODULE  for the  Modulate t=  54.00000

MODULE  overshoot   Modulate t=  58.00000

MODULE   fail for  Modulate t=  62.00000

MODULE   successful for the Modulate t=  66.00000

Upvotes: 2

NeronLeVelu
NeronLeVelu

Reputation: 10039

incremental

awk '/t=[[:blank:]]*0.00000/{gsub(/t=[[:blank:]]*0.00000/, "t= " (Inc+=50) ".00000")}1' YourFile > NewFile

you can add BEGIN{Inc=-50} as first action if first occurence of t=0 need to still be 0

decal

sed '/\(t=[[:blank:]]*\)0\([.]00000\)/ s//\150\2/' YourFile > NewFile

Upvotes: 0

Related Questions