Pramod Pramod - 1 year ago 79
Linux Question

vi command to copy certain lines to another file

I have a log file with thousands of lines. I want to copy certain logs ranging between two line numbers to another file so that I can share them within team.

For example - I want to copy text from line number 1000 to line 1200 to another file say issueLog.log

What is the shortest way to do this?

Note: We are not using vim.

Answer Source

Open the original file in Vi and type the following command.

:1000,1200w issueLog.log


Use GNU sed

sed -n 1000,1200p original-file > issueLog.log


Use perl

perl -ne 'print if 1000..1200' original-file > issueLog.log


Use awk

awk 'NR>=1000&&NR<=1200' original-file > issueLog.log
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download