Skip to main content
added 82 characters in body
Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

You can write an addressed range of lines to a new file, and then delete the range - the tricky part is preventing the d command from being treated as part of the output file name. In GNU sed, you can do that by splitting the write and delete into separate expressions using -e

IMPORTANT NOTE: this will truncate otherfile rather than appending to it

sed -i.bak -e '/string/,${w otherfile' -e 'd;}' file 

You can write an addressed range of lines to a new file, and then delete the range - the tricky part is preventing the d command from being treated as part of the output file name. In GNU sed, you can do that by splitting the write and delete into separate expressions using -e

sed -i.bak -e '/string/,${w otherfile' -e 'd;}' file 

You can write an addressed range of lines to a new file, and then delete the range - the tricky part is preventing the d command from being treated as part of the output file name. In GNU sed, you can do that by splitting the write and delete into separate expressions using -e

IMPORTANT NOTE: this will truncate otherfile rather than appending to it

sed -i.bak -e '/string/,${w otherfile' -e 'd;}' file 
Source Link
steeldriver
  • 83.9k
  • 12
  • 124
  • 175

You can write an addressed range of lines to a new file, and then delete the range - the tricky part is preventing the d command from being treated as part of the output file name. In GNU sed, you can do that by splitting the write and delete into separate expressions using -e

sed -i.bak -e '/string/,${w otherfile' -e 'd;}' file