-4

I'm looking for a way to do this with sed or perl. I have files filled with strings like this:

0,0,0:17
1,0,0:17
0,1,0:17
0,0,1:17
1,0,1:17

I also have files filled with strings like this:

0,0,-1:17.12
1,0,-1:17.12
2,0,-1:17.12
0,1,-1:17.12
1,1,-1:17.12

I want to change all instances of the number 17 into 17.12, but I don't want to accidentally change instances of 17.12 into 17.12.12.

Any help?

dlamblin
  • 40,676
  • 19
  • 92
  • 127
  • You're effectively asking for a regular expression, read: http://stackoverflow.com/tags/regex/info to see available online tools that could help you rapidly test your scenario. – dlamblin Apr 13 '16 at 17:49
  • 2
    I'm voting to close this question as off-topic because the scope is too narrow and will only help one person once with a specific regular expression. – dlamblin Apr 13 '16 at 17:50

3 Answers3

1

With GNU sed:

sed 's/:17$/:17.12/' file

Update:

sed -r 's/:17([^.]|$)/:17.12/' file

If you want to edit your file "in place" use sed's option -i.


See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117
0

One way to do it is with a Perl negative look-ahead assertion ((?!pattern), see the 'perlre' man page for details):

perl -ple 's/\b17\b(?!\.12\b)/17.12/' file

Use

perl -i -ple ...

to update the file.

This will replace '17' with '17.12' anywhere in the file as long as it is not already followed by '.12'. It may be overkill for this problem, but the technique is worth knowing.

pjh
  • 2,848
  • 11
  • 15
0

Given that 17 occurs at the end of a line, you can anchor your pattern there:

s/17$/17.12/
Sinan Ünür
  • 113,391
  • 15
  • 187
  • 326