LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 02-17-2011, 05:22 PM   #1
mgarciagarcia1982
LQ Newbie
 
Registered: Feb 2011
Posts: 1

Rep: Reputation: 0
Cat command in bash


Hello,

how can I set the cat command to read specified lines of a text file,

like if I have a text file with 100 lines, who can I say cat only line 23 to 42?
 
Old 02-17-2011, 05:46 PM   #2
jcalzare
Member
 
Registered: Aug 2009
Location: Chicago
Distribution: CentOS
Posts: 114

Rep: Reputation: 34
You want to use sed.

This prints line 23:
sed -n '23p' file.txt

This prints a range, including lines 23 and 42:
sed -n '23,42p' file.txt
 
Old 02-17-2011, 07:52 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -ne 'print if $.>=23 && $.<=42' file
 
Old 02-17-2011, 11:39 PM   #4
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
Quote:
Originally Posted by mgarciagarcia1982 View Post
Hello,

how can I set the cat command to read specified lines of a text file,

like if I have a text file with 100 lines, who can I say cat only line 23 to 42?
Another non-elegant way would be:
Code:
head -n 42 your-file | tail -n 20
to get 20 lines before and including 42, i.e., 23-42.
 
  


Reply


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
a little bash help: cat multiple files together except first line? sanimfj Linux - General 3 02-09-2010 05:27 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
Bash - Strip Spaces from string then cat cmfarley19 Programming 8 07-25-2004 01:01 PM
Is this a valid command? find ./ -name bash.bashrc | cat davidas Linux - Newbie 11 04-06-2004 03:16 AM
cat Command Moeses Linux - General 1 06-20-2002 04:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:35 PM.

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