The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern matching in file and then display 10 lines above every time namishtiwari Shell Programming and Scripting 16 4 Weeks Ago 07:50 PM
Display lines of the file on a log sbryant Shell Programming and Scripting 2 02-26-2008 11:16 AM
display all lines shary Shell Programming and Scripting 3 02-17-2007 01:09 PM
display no of empty lines atticus Shell Programming and Scripting 5 03-22-2006 12:35 PM
display a portion of lines from file champion UNIX for Dummies Questions & Answers 6 09-20-2005 08:11 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-25-2004
Registered User
 

Join Date: Mar 2002
Location: Bangalore,Karnataka State,India
Posts: 18
display few lines of the file

Hi,

If I want to have a look at few lines of the file, how do I, what command to use.

Eg: If I have a file having length 2000 lines and I want to have a look at the content between 1400 and 1600, How do I look at it ?

Also, If I want to have a look at function alone in a file, how do I go about it ?

Eg: If I have a file with 7 functions and Main part. And one function is :

sub sample
{

----
----
----
return 1;
}

And If I want to get only the contents of this function from file, How do I get it ?


Thanks,
Sharath
__________________
K.S.SHARATH CHANDRA
Reply With Quote
Forum Sponsor
  #2  
Old 04-26-2004
Kelam_Magnus's Avatar
Registered User
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
I tend to agree with Driver...

If you can read a 2000 line script with Functions in it... you should know how to use "vi" and file manipulation to view a file.


Please, defend your question, or I might have to close this post.


Here is a great link for Vi related questions.

http://www.thomer.com/vi/vi.html
__________________
My brain is your brain
Reply With Quote
  #3  
Old 04-26-2004
Registered User
 

Join Date: Mar 2002
Location: Bangalore,Karnataka State,India
Posts: 18
Hi Driver,
This is not a HOMEWORK question.

Head has one option -n to display first n number of lines in a file.
Which won't serve my purpose.

Tail does samething from opposite direction and hence no use either.

I wanted a combination to get a range of lines.
More specifically, I wanted to extract a function block from the file.

( I have around 20 similar programs written in perl and all have few common functions. I need to change these functions now. In order to verify I didn't want to open again using "vi". Hence the need was to a have look at the function block in all files and verify the changes.)

I didn't get what Kelam_Magnus is saying.

I have gone through the link given on Vi few times long back and thanks for reminding me on the same. I will go through the page again as it gives lot of pleasure in reading the same.

Thanks,
Sharath
__________________
K.S.SHARATH CHANDRA
Reply With Quote
  #4  
Old 04-27-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
Re: display few lines of the file

You can use awk start/end pairs too....
Quote:
I want to have a look at the content between 1400 and 1600, How do I look at it ?
Just use...
awk 'NR==1400,NR=1600' file1
Quote:
sub sample
{

----
----
----
return 1;
}

And If I want to get only the contents of this function from file, How do I get it ?
Try...
awk '/sub sample/,/}/' file1
Reply With Quote
  #5  
Old 04-27-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
Driver,

My point was that in awk you can use start/end pairs. i.e. two patterns or conditions sepated by a comma. All you have to do is determine what the conditions are.

My post works with the sample supplied. But with the sample you have supplied, the end condition might be "/^}$/" , i.e. a line that is one "}" on its own with no whitespace. So you would have...

awk '/sub foo/,/^}$/' file

...which could be the simplest solution, but perhaps more work is needed.

It would be more efficient to use "exit" to stop reading the file when the end condition is reached. The idea of counting matched braces can be coded more simply as ....

awk '/sub foo/,0 {print;b+=gsub("\{","{");b-=gsub("\}","}");if(b==0)exit}' file
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:09 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0