LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-05-2010, 03:41 PM   #1
X.Cyclop
Member
 
Registered: Jun 2006
Location: Tlv
Distribution: Arch!
Posts: 120

Rep: Reputation: 21
grep search between n-m


How can i use grep (or any other command) to check for lines that begin with N number.

E.g. I want to print out commands (from history), but only from the command number 50 until #200.

This one doesn't work:
Code:
history | grep "^[50-200]"
Should print out something like:
Code:
50 cd ~/Desktop
51 ls -lh
..........
199 gconftool-2 --set /apps/metacity/global_keybindings/panel_main_menu --type string "Super_L"
200 find . -name "x" -type f -size +100M

Last edited by X.Cyclop; 10-05-2010 at 03:44 PM.
 
Old 10-05-2010, 03:56 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,610

Rep: Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518Reputation: 518
grep doesn't work like that. grep is looking for patterns, not ranges of lines. You could construct a giant regex to only get those numbers, but that's a lot of work.

You can use head/tail or sed or awk to get only certain lines, for example
Code:
history | sed -n '50,200p'
Of course, the results may not actually start with 50-200, but they will be lines 50-200.

Last edited by AlucardZero; 10-05-2010 at 03:58 PM.
 
1 members found this post helpful.
Old 10-05-2010, 04:05 PM   #3
X.Cyclop
Member
 
Registered: Jun 2006
Location: Tlv
Distribution: Arch!
Posts: 120

Original Poster
Rep: Reputation: 21
Great! thanks. I thought i wouldn't need sed, but now i see that i must learn it.
 
Old 10-05-2010, 07:17 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by X.Cyclop View Post
I thought i wouldn't need sed, but now i see that i must learn it.
its true, you don't need sed. all you need to learn is awk. It does the same function as sed and much more.
Code:
awk 'NR>50{print}NR>100{exit}' file
NR means "record number so far"
 
1 members found this post helpful.
Old 10-06-2010, 04:28 AM   #5
X.Cyclop
Member
 
Registered: Jun 2006
Location: Tlv
Distribution: Arch!
Posts: 120

Original Poster
Rep: Reputation: 21
Maybe both? i see sed a bit easier and shorter.
 
  


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
how to search files apart from GREP?? kapilbajpai88 Linux - Newbie 6 07-24-2008 11:15 AM
grep search... tobiasw Linux - General 4 10-28-2006 01:36 PM
To recursively search with grep grautu Slackware 5 11-21-2005 02:53 AM
can you specify which files to grep search? sneakyimp Linux - Software 4 10-12-2005 08:28 PM
Using Grep to search all sub-directories yrraja Solaris / OpenSolaris 2 08-28-2004 03:29 AM


All times are GMT -5. The time now is 07:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration