I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB)
eg:
$(AA) something
$(AA) $(BB) something
I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB)
eg:
$(AA) something
$(AA) $(BB) something
This is trivial with ed
. Just use the global-non-matched command, v
, to mark every line that does not contain $(BB), then perform the substitution, s
, on those lines.
Regards,
Alister
Is it possible with sed or awk? I want directly change the file
Lei
I'm sure it is, but ed
is the more logical choice for editing a file (no need to explicitly write to a second file which then needs to clobber the original).
Regards,
Alister
Actually I don't know how to use ed to do this, I want it add into my script
Ed example:
v/bb/s/aa/aa bb/g
in all lines without 'bb' sustitute 'aa' with 'aa bb'
Use in code like this:
ed filename <<eof
v/bb/s/aa/aa bb/g
w
q
eof
Even some implementations of ed may create a temp file.
It's never a good thing to edit your originals. If you screw up, you've lost data.
That temp file is just a scratch file. With ed, the buffer is the source of all writes (allowing for the preservation of inode, hardlinks, softlinks, etc). Unlike gnu sed -i/perl -i which create-rename/rename-create, respectively, with their temp files.
That's what backups are for. Further, if you want to be safe, you can trivially create a copy before making any changes. Either with cp or from within ed.
ed -s "$fname" <<EOED
w $fname.backup
... editing commands here ...
w $fname
q
EOED
If the first write fails for any reason, since stdin is not an interactive device, ed will abort and the edits will not occur.
Regards,
Alister