The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to add line numbers in text file suryanarayana Shell Programming and Scripting 4 01-31-2009 01:59 PM
reading line by line and grepping QueryMaster Shell Programming and Scripting 4 01-30-2008 05:12 PM
find 2 line numbers, grab text in between Da_Duck Shell Programming and Scripting 11 09-26-2006 12:51 PM
how to print out line numbers of a text file? forevercalz Shell Programming and Scripting 4 12-12-2005 05:04 AM
grepping for numbers in between fusion99 UNIX for Dummies Questions & Answers 1 11-12-2004 04:07 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-22-2009
anushree.a anushree.a is offline
Registered User
  
 

Join Date: May 2008
Posts: 87
Grepping text by providing line numbers.

Dear Friends,
I have a flat file from which I want to grep line no. 7,10, 19 to 35, 37.
How can it be done?

Thank you in advance
Anushree
  #2 (permalink)  
Old 06-22-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,047
Hi.

When you say "grep lines", do you mean extract lines?

There are a number of tools that can do this.

But based on what you asked, this should work...

sed "7p;10p;19,35p;37p;d" my_file | grep "....."
  #3 (permalink)  
Old 06-22-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,854
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'NR~/^7|1[09]|2[0-9]|3[0-5,7]$/' infile

Last edited by radoulov; 06-22-2009 at 11:04 AM..
  #4 (permalink)  
Old 06-22-2009
durden_tyler's Avatar
durden_tyler durden_tyler is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 523
One way to do it in perl:

Code:
perl -ne 'BEGIN{foreach $i (7,10,(19..35),37){$x{$i}=1}}{print if $x{$.}==1}' infile
tyler_durden
  #5 (permalink)  
Old 06-22-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,854
Quote:
Originally Posted by durden_tyler View Post
One way to do it in perl:
[...]
Code:
(19..35)...
I have to read more carefully,
I corrected my post.

Using the same logic with Perl:

Code:
perl -ne'$.=~/^7|1[09]|2[0-9]|3[0-5,7]$/and print' infile

Last edited by radoulov; 06-22-2009 at 11:09 AM..
  #6 (permalink)  
Old 06-22-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,521
Code:
awk 'NR ~ /^(7|10|19)$/' file
  #7 (permalink)  
Old 07-01-2009
anushree.a anushree.a is offline
Registered User
  
 

Join Date: May 2008
Posts: 87
Hey friends
Almost all solutions worked fine
Thank you for the quick reply.
Take care.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0