You cannot use the redirection operator ( > or >> ) for the same file, because it has a higher priority, and it will create / trim the file before the command is even called. To avoid this, you should use appropriate tools such as tee , sponge , sed -i or any other tool that can write results to a file (for example, sort file -o file ).
In fact, redirecting input to the same source file does not make sense, and you should use the appropriate editors in place for this, for example, the Ex editor (part of Vim):
ex '+g/seg[0-9]\{1,\}\.[0-9]\{1\}/d' -scwq file_name
Where:
'+cmd' / -c - run any Ex / Vim commandg/pattern/d - delete lines matching the pattern using global ( help :g )-s - silent mode ( man ex )-c wq - execute :write and :quit commands
You can use sed to achieve the same (as already shown in other answers), however, the -s tandard extension of FreeBSD is not in place of ( -i ) (it can work differently between Unix / Linux) and basically it is s Tream ed Itor, not a file editor. See: Is there a practical Ex-mode?
kenorb Apr 18 '16 at 9:45 2016-04-18 09:45
source share