1

Say I have a file with 2 rows, named file.txt:

Row 1: 80 is a nice number
Row 2: 80 is a cool number
Row 3: 80 is a piquant number
Row 4: 80 is a sexy number

I did sed -i 's/80/8080/g', but by mistake I ran it twice and the result in all rows was 80808080 instead of just 8080.


I believe it is most likely impossible in the common technology but I still want to ask if maybe, there is a sophisticated way to make sed do it's effect only once on this file. I mean, some kind of "lock" that will avoid happening of such mistakes like running this sed more than one time one the file, until I remove tat lock. This might (and I emphasis the word "might") be helpful when dealing with much longer rows, especially in large amounts.

It's not really a problem and I should avoid such a mistake and that's it, but I am curious to know if someone try to develop some technical "defense mechanism" from such sed mistakes.

Note: I believe a similar solution could be helpful to awk users as well.

  • 10
    don't use -i until you're sure? – Jeff Schaller May 10 '17 at 18:53
  • 1
    aren't there 4 lines/rows in the sample file? – Jeff Schaller May 10 '17 at 18:53
  • 4
    don't use /g unless you want every match in the line changed – Jeff Schaller May 10 '17 at 18:53
  • 1
    This is a training / experience problem, not a technical problem. If you don't want to run sed twice, then be careful enough to not run it twice. – John May 10 '17 at 19:12
  • 5
    You could write the regex so that it doesn't match the result of its own substitution - e.g. using word boundaries s/\b80\b/8080/ – steeldriver May 10 '17 at 19:13
0

Just don't use -i for in-place editing, especially if you're trying something out or developing some script. Instead, always write to a temporary file. Then check the file to see if it's ok, and possibly replace the original file with it.

The -i option is non-standard and is horribly non-portable between sed implementations.

The portable way to change a file using sed is

sed 'expression' file >newfile && mv newfile file

awk does not have the same issue. None of GNU awk, mawk or nawk supports in-place editing (which that isn't really something one often wants to do with awk anyway), so the problem of accidentally running the same awk script over a file twice (and getting twice the "effect" of editing the file) very seldom arises.

2

This isn't very sophisticated, but it is a sort of a defense mechanism, when substituting a string with a string that contains itself is to add the previous and/or next characters to prevent duplicate substitution.

Per your example above I recommend something like one (or both) of the following:

  • sed -i 's/ 80 / 8080 /g'
  • sed -i 's/^80 /8080 /g'

Also, be careful with -i :)

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.