display few lines of the file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers display few lines of the file
# 1  
Old 04-26-2004
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
# 2  
Old 04-26-2004
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
# 3  
Old 04-27-2004
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
# 4  
Old 04-27-2004
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
# 5  
Old 04-27-2004
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To display last 5 lines of a file

Hi Guys, I want to echo last 5 lines of a file to a mail. My script getting continuously looped and not getting the output. can anyone help? #!/bin/bash read karthick; tail -5 $karthick; echo $karthick | mail -s "genius" someone@gmail.com Thanks NK (2 Replies)
Discussion started by: Karthick N
2 Replies

2. Shell Programming and Scripting

Display specific lines content from the file

Hell, I want to grep certain word from file and display above 2 lines and after two lines. Here is the content of sample file. Mar 14, 2013 12:56:59 AM Agent.Agent SendTo INFO: Connection to server:7041 - Credential Transmit Successesful Mar 14, 2013 8:54:21 AM cgent SendTo WARNING:... (7 Replies)
Discussion started by: balareddy
7 Replies

3. UNIX for Dummies Questions & Answers

Script to display lines in a file in a particular format

I have a bunch of files in various folders. I want to go through each of them and display certain lines in a particular format All files have a similar format Date: Time: User: Message: Miscellaneous: (and some other stuff)I want to display to only the "Date:", "Time:" "User:" lines in... (7 Replies)
Discussion started by: newbiegal01
7 Replies

4. UNIX Desktop Questions & Answers

How to display only the lines from a file which do not contain a given number

a. How do I display the content of the file containing what Ive merged using a filter which would display only the lines of the file which don't contain number, for example 3 or 6. (3 Replies)
Discussion started by: herberwz
3 Replies

5. UNIX for Dummies Questions & Answers

Display only the first two characters of all the lines from a file.

how do i Display only the first two characters of all the lines from a file.? (1 Reply)
Discussion started by: ritusubash
1 Replies

6. Shell Programming and Scripting

Parse a file to display lines containing a word

Hi! I'm trying to create a shell script to parse a file which might have multiple lines matching a pattern (i.e. containing some word). I need to return all lines matching the pattern, but stripping the contents of that line until the pattern is matched For example, if my input file was ... (4 Replies)
Discussion started by: orno
4 Replies

7. Shell Programming and Scripting

grep and display lines from a file

I have to grep on a few words in a file and then display the line containing those words and the line above it. For ex - File1.txt contains... abc xyz abc This is a test Test successful abc xyz abc Just a test Test successful I find the words 'Test successful' in the file... (6 Replies)
Discussion started by: user7617
6 Replies

8. Shell Programming and Scripting

Display file without # lines

Hi to all in this great forum, im sure this has been asked lots of times before but ive been looking for the past day and cant find the answer. I use cat/some/file to display its contents but how can i get it to not display hashed out lines, or do i need another command, Thanks in advance:) (5 Replies)
Discussion started by: dave123
5 Replies

9. Shell Programming and Scripting

Display lines of the file on a log

Hi, I am using the Korn shell script to display lines of the file. For example below: outputfile=test.dat -- display each line of the test.dat file abcd 345 adek 45566 dve3 34565 so on... I appreciate your time to find a command for displaying lines of the file on the log. ... (2 Replies)
Discussion started by: sbryant
2 Replies

10. UNIX for Dummies Questions & Answers

display a portion of lines from file

This is truly dummy question. I have a text file of 100 lines. What unix commnad to extract line 20 to 40 and output it to another file? Is it something cat or grep or >> ? Thanks (6 Replies)
Discussion started by: champion
6 Replies
Login or Register to Ask a Question