Reputation: 53
I have a text file which has millions of lines. I wanted to extract texts between some X to Y lines. How I can achieve this effectively.
Which is the best method to do this.
Thanks A lot.
Upvotes: 0
Views: 109
Reputation: 195209
if your file is really huge, I would do: (assume x, y are start and end line numbers)
sed -n 'x,yp;yq' file
or
awk 'NR==x{p=1}NR==y+1{exit}p' file
The above commands will stop further processing after reaching line y
. It may save you some time.
Upvotes: 2
Reputation: 1858
you can use head and tail.
head -n Y yourfile | tail -n (Y-X+1)
you should replace (Y-X+1) with the number.
Upvotes: 0
Reputation: 2539
@rahul answer is correct. Alternatively, you can use head and tail in combination:
tail -n +x input.txt | head -n y > output.txt
This time, tail -n +x prints out the entire file starting from line x, and head -n y prints the first y+1 lines of that . It's redirected to output.txt in the same way.
Upvotes: 1
Reputation: 172568
You can try this:
sed -n x,yp yourfile > newfilename
Just replace x and y with the range of lines from which you want to extract contents.
Upvotes: 2