Computing.Net > Forums > Unix > Searching by line number

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Searching by line number

Reply to Message Icon

Name: Jim
Date: December 1, 2002 at 22:10:43 Pacific
OS: UNIX
CPU/Ram: 512
Comment:

Hello,

I have a data file I'm searching. I know the line numbers that the data I want is in. I now want to extract only those lines in the file and output them to a new file. How do I do that using grep or awk. Thank you very much in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: December 2, 2002 at 04:48:46 Pacific
Reply:

You can use head and tail. To pull line 1000

tail -1000 filename | head -1

Sed will also let you match a line #
http://www.gnu.org/manual/sed-3.02/html_chapter/sed_3.html#IDX25

grep with the -n will give you line numbers that you can then match as a starting pattern

grep -n "." /etc/passwd | grep '^30:' | cut -f 2- -d ':'

the above will use the '.' as a wild card matching all, the -n flag to grep will prepend line numbers. The '^30:' says give me line 30. The cut portion says to peel off the line number and present the balance.


0

Response Number 2
Name: Jim
Date: December 2, 2002 at 12:47:20 Pacific
Reply:

Thanks David,

You were a big help to me :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Searching by line number

awk line number www.computing.net/answers/unix/awk-line-number/7722.html

Reading a file line by line www.computing.net/answers/unix/reading-a-file-line-by-line/5633.html

grep to find a specific line number www.computing.net/answers/unix/grep-to-find-a-specific-line-number/6484.html