Get selected line for Text File

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
shakirz1
Battalion Quarter Master Havaldaar
Posts: 207
Joined: Sat Aug 09, 2003 5:00 pm
Location: Karachi
Contact:

Get selected line for Text File

Post by shakirz1 »

I want to get select line from a text file, which linux command will work for it? means I want to get line 1-10 from abc.txt
jargon
Lieutenant Colonel
Posts: 691
Joined: Mon Oct 13, 2003 9:40 am

Post by jargon »

$: head abc.txt

the head command by default grabs the first 10 lines.

use head -n 15 abc.txt if you want the first 15, and so on.

Read "man head"
jargon
shakirz1
Battalion Quarter Master Havaldaar
Posts: 207
Joined: Sat Aug 09, 2003 5:00 pm
Location: Karachi
Contact:

Get selected line for Text File

Post by shakirz1 »

And what I do if I need line from 20-30 ? I think "head" will not work for it.
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re: Get selected line for Text File

Post by LinuxFreaK »

Dear shakirz1,
Salam,
shakirz1 wrote:And what I do if I need line from 20-30 ? I think "head" will not work for it.
head + tail will work :)

# tail -f filename

Best Regards.
Farrukh Ahmed
s7r1k3r
Battalion Quarter Master Havaldaar
Posts: 221
Joined: Wed Aug 07, 2002 3:02 pm
Location: Rawalpindi

awk to the rescue.

Post by s7r1k3r »

Assalam-O-Alaekum!

You can use awk for the purpose. Here is the command to solve your problem.

awk 'NR>10 && NR<21 {print}' abc.txt

Obviously, you can change the no.s 10 and 21 to select any range.

Regards.
a10n3 s7r1k3r
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: awk to the rescue.

Post by lambda »

s7r1k3r wrote:You can use awk for the purpose.
finally! a sane post.
Post Reply