0

i have this :

sed -e "/$name/s/[0-9]\:*$/$P/" -i sales_info.txt 

When i use the above to change this : My text file format name:no:no:type:p

Tom:8:987654:commission:0
Tommy:7:12379813:hourly:0
Fan:5:2132131:salaried:0

assuming $name = Tom , $P = 55.5

i get this :

Tom:8:987654:commission:0
Tommy:7:12379813:hourly:55.5
Fan:5:2132131:salaried:0

However if i try to change the $P from 55.5 to 41.5

it becomes

Tom:8:987654:commission:0
Tommy:7:12379813:hourly:55.41.5
Fan:5:2132131:salaried:0

I have tried :

sed -e "/$name/s/\:[0-9]*$/\:$P/" -i sales_info.txt 

It only changes the value from :0 If the value is more than 0 , it won't change at all. Assuming if i would insist to use sed for this . have i place the : in the wrong position ? Im trying to change the last column value after :

  • AFAIK : doesn't need to be escaped here. However since . isn't in the range [0-9], the pattern [0-9]* won't match decimals like 55.5 - you'd need something like [0-9.]* i.e. "/$name/s/:[0-9.]*$/:$P/" – steeldriver Nov 4 '16 at 2:09
  • hi , actually i tried the . too , it removed everything except the name which means name:no:no:type:p to name:p @steeldriver – what Nov 4 '16 at 2:12
  • Are you sure you put the . inside the brackets i.e. [0-9.]* not outside like [0-9].*? – steeldriver Nov 4 '16 at 2:15
  • thanks it work . would like u to answer so i could accept it – what Nov 4 '16 at 2:17
  • Note that you should also match name to word boundaries, e.g. /\b$name\b/ – Thor Nov 4 '16 at 2:20
0

Answered in the comments:

AFAIK : doesn't need to be escaped here. However since . isn't in the range [0-9], the pattern [0-9]* won't match decimals like 55.5 - you'd need something like [0-9.]* i.e. "/$name/s/:[0-9.]*$/:$P/"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.