Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-24-2009, 04:10 PM   #1
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Rep: Reputation: 19
Print only specific rows in a text file

Hi there,
thanks for your help with my questions yesterday!

I have a simple one now, I guess. Yet, getting a solution through Google with key words such as "awk, print, rows" seemed hard.

Here is my question:

I have a text file with 100 rows. I want to make "new_file1.txt" containing rows 1-10, "newfile_2.txt" containing rows 11-20, etc.

(By the way, each row contains a long string of characters, so there's not a clear column structure)

Thanks in advance,


Last edited by Mike_V; 04-24-2009 at 04:25 PM.
Old 04-24-2009, 05:07 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

To print lines 1 through 10 (for example):
sed '1,10!d' INFILE > OUTFILE
'Hope that helps .. PSM

Here's a great sed "cheat sheet":
Old 04-24-2009, 05:33 PM   #3
Senior Member
Registered: Nov 2005
Distribution: Arch
Posts: 3,093

Rep: Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273Reputation: 1273
The split program is written for exactly this task:
split --lines=10 --numeric-suffixes oldfile.txt newfile_

# if you want .txt extension
for f in newfile_* ; do
    mv $f $f.txt
Old 04-24-2009, 07:18 PM   #4
Registered: Apr 2009
Location: Boston MA
Distribution: CentOS 6.2 x86_64 GNU/Linux
Posts: 59

Original Poster
Rep: Reputation: 19
You guys are awesome!


awk, print

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare two fields on consecutive rows and print the two rows aditi_borkar Linux - Newbie 3 04-09-2009 05:49 AM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
deleting 100 rows in text file (1-100) via terminal itz2000 Linux - Newbie 5 01-17-2006 11:19 PM
SED - display text on specific line of text file 3saul Linux - Software 3 12-29-2005 04:32 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM

All times are GMT -5. The time now is 02:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration