Suppose I want to cut more than one interval of lines from one file (eg., lines 1-500, 1029-1729 and 2696-3446), appending values at the end of another file (output.txt) and eliminating these values from the first file. The origin is a file with 9277 lines, I want to cut some of them, eliminating them from the original file and pasting them into another file. Is that possible via command line?
Ubuntu – Cut specific lines of a file and paste them at the end of another file
command linetext processing
Related Question
- Ubuntu – Remove any trailing blank lines or lines with whitespaces from end of file
- Ubuntu – How to print values in a text file to columnated file using shell script
- Ubuntu – How to copy the content of a text file and paste it to another starting at a certain line
- Ubuntu – How to get specific information from two text lines and assemble a filename from that
Best Answer
Using sed, you can write a set of lines to a different file while deleting it from the current file like so:
where
N
andM
are the line numbers. The-i
option makessed
save changes to the source file, and here thed
command deletes to those lines. At the same time,w output.txt
causes the selected lines to be written tooutput.txt
. And yes, those are two separate lines:sed
requires that thew
command's filename be till a newline.So you can do something like: