How to read contents of a file from a given line number upto line number again specified by user


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to read contents of a file from a given line number upto line number again specified by user
# 1  
Old 07-13-2010
How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone.

I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below:

Code:
if [ $# -eq 3 ]; then
        if [ -e $3 ]; then
            tail +$1 $3 | head -n $2
         else
            echo "$0: Error opening file $3"
            exit 2
        fi
else
        echo "Missing arguments!"
fi

Problem with this code is that it always returns lines from the bottom of the file. I even exchanged the commands head and tail, but of no use. While compiling the above code it shows the following:

Code:
tail: cannot open `+3' for reading: No such file or directory
==> /home/chandran/JONAS/LoveSick <==
It's every day, it's every night
O-oh, call the the doctor
I'm love sick

Can anyone please help me out with the solution.

Thank you.
# 2  
Old 07-13-2010
Code:
sed -n '3,5p' filename

These 2 Users Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-13-2010
Just use sed:

Code:
% min=3 max=5
% sed -n $min,${max}p infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 07-13-2010
how to get the solution using command line arguments?

Thank you all for the reply.

I am passing the parameters as command line arguments. In that case how do I get the desired result?

Thank you.
# 5  
Old 07-13-2010
Code:
% cat display_range.sh 
#!/bin/sh

sed -n "$1,${2}p" "$3"
% chmod u+x display_range.sh 
% print -l {1..10} > infile
% cat infile 
1
2
3
4
5
6
7
8
9
10
% ./display_range.sh 3 7 infile 
3
4
5
6
7

This User Gave Thanks to radoulov For This Post:
# 6  
Old 07-13-2010
Thank you..

I got it..

---------- Post updated at 04:44 PM ---------- Previous update was at 04:42 PM ----------

Thank you people, I finally got the solution after all your help.

The code should be like this:

Code:
if [ $# -eq 3 ]; then
        if [ -e $3 ]; then
             sed -n "$1, ${2}p" "$3"
                # tail +$1 $3 | head -n $2
         else
            echo "$0: Error opening file $3"
            exit 2
        fi
else
        echo "Missing arguments!"
fi

It works.
I thank you all once again.

Regards
G Ravi Chandran
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding user name to file, and then displaying new line number

Hi all - I'm completely stumped by a script I'm working on... The short version is I have a file called 'lookup' and in it are hundreds of names (first and last). I have a script that basically allows the user to enter a name, and what I need to have happen is something like this: Record... (8 Replies)
Discussion started by: sabster
8 Replies

2. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

3. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

5. Shell Programming and Scripting

Read flat file upto certain number of columns

Hello Guys Please help me with the below issue I want to read a flat file source upto certain number of columns Say my flat file has 30 columns but I want to read upto 25 columns only How come the above issue can be addressed? Thanks a lot!!!! (1 Reply)
Discussion started by: Pratik4891
1 Replies

6. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

7. Shell Programming and Scripting

Read the specified line number from file

Hi Guys, I am new to unix. Actually i want help in writing an single command where i can actually read specific line number in file where the line number will be passed to command as parameter. ex. 1 a 2 b 3 c 4 d And to my command i pass as 2. so i should get output as 2 b ... (15 Replies)
Discussion started by: kam786sim
15 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question