0

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?

3

Using sed, you can write a set of lines to a different file while deleting it from the current file like so:

sed -i -e 'N, M { w output.txt
d }' input.txt

where N and M are the line numbers. The -i option makes sed save changes to the source file, and here the d command deletes to those lines. At the same time, w output.txt causes the selected lines to be written to output.txt. And yes, those are two separate lines: sed requires that the w command's filename be till a newline.

So you can do something like:

cmd=' { w output.txt
d }'
sed -i -e "1,500 $cmd" -e "1029,1729 $cmd" -e "2696,3446 $cmd" input.txt
1

possible !

  1. copy lines to dest.file with redirected outout of grep 'pattern' src.file >> dest.file

  2. delete lines from src.file with sed -i '/pattern/d' src.file

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.