Visit Our UNIX and Linux User Community

How to read a file starting at certain line number?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a file starting at certain line number?
# 1  
Old 07-29-2015
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!
# 2  
Old 07-29-2015
Hello dcowboys13,

Welcome to forums, hope you will enjoy the learning/knowledge sharing here. Following is an example which may help you in same, you can take it as a start up and then can do some efforts from your side. let us know then where you get stuck so that we may guide you then, as we all are here for learning and helping.
Let's say we have following input file.
 cat test1

We want to read from 5th line out of 12 lines in input file. Then following is the script for same.
 cat test.ksh
 awk -vNUM=`echo $number` '(NR>=NUM)' test1
 cat test.ksh
 awk -vNUM=5 '(NR>=NUM)' test1

Output will be as follows.

Hope this helps.

R. Singh
# 3  
Old 07-29-2015
Really depends on what you plan to do next.

tail -n +"$lineNumber" "$file" | while read line; do

# 4  
Old 07-29-2015
It depends what you mean by " a file line by line from a certain line number."

The command tail with appropriate options might give you what you want. Can you elaborate on the requirement? Perhaps something like:-
tail +$lineNumber file

You can pass this into a loop line this:-
tail +$lineNumber file | while read line
    ..... # Whatever you need

You can make it more elaborate if you have multiple fields and/or field separators, so if it was colon separated : and you want to read five fields, you can:-
tail +$lineNumber file | while IFS=: read f1 f2 f3 f4 f5 
    ..... # Whatever you need

However, if you are just after a single line, you could use:-
sed -n${lineNumber}p file

Beware that this will read a single line and is therefore very IO intensive if you want to read multiple time from a file working down it.

I hope that these structures help. Can you explain a little more about the requirements?

# 5  
Old 07-30-2015
Longhand using builtins only and has a start and finish to select any or all lines.
If you have no idea how many lines there are then just give $2 a rediculously large value, say 1000000...
OSX 10.7.5, default bash terminal.
# line_no
# Usage: line_no start finish /full/path/to/filename /full/path/to/outfile
# Note:- There is no user error correction code here so don't make any typos.
# Working part.
while read -r line
	if [ $n -ge $1 ] && [ $n -le $2 ]
		echo -E $line
done < $3 > $4
# End of working part.
cat $4

Last login: Thu Jul 30 22:11:35 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./line_no 1 1 ~/ /tmp/text
AMIGA:barrywalker~/Desktop/Code/Shell> ./line_no 3470 3489 ~/ /tmp/text
#               |        /    --+--     /         | +     /      ------------
#               |     R1 \     / \ D1   \ R2     === C2   \ R3
#               |        /    '---'     /        -+-      /
#               |        \      |       \         |       \ 
#               |        /      |       /         o       /
#               |        |      |       |         |       |       Y
#               +--------o------o-------o---------o---o---o-------O -VE.
#                              Pseudo Ground. ----> __|__
#                                             _GND_ /////
# Parts List:-
# C1 ......... 1 uF, 50V.
# C2 ......... 10 uF, electrolytic, 10V, IMPORTANT! SEE TEXT.
# R1 ......... 33R, 1/8W, 5% tolerance resistor.
# R2 ......... 1M, 1/8W, 5% tolerance resistor.
# R3 ......... 100K, 1/8W, 5% tolerance resistor.
# D1, D2 ..... OA90 or any similar germanium diode.
# 3.2mm standard STEREO jack plug for headset socket.
# Coaxial connecting cable.
# Sundries as required, stripboard, etc, (similar to above).
# #########################################################
AMIGA:barrywalker~/Desktop/Code/Shell> _

Hmm, just thought my Android phone does not have commands like 'tail', etc, so this could be useful...
Decided to remove variable names and use $1, $2, $3 and $4 directly instead.

Last edited by wisecracker; 07-31-2015 at 04:15 AM.. Reason: See above.

Previous Thread | Next Thread
Test Your Knowledge in Computers #796
Difficulty: Medium
Fractions in binary arithmetic terminate only if 2 is the only prime factor in the denominator.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to grep a line not starting with # from a file (there are two lines starting with # and normal)?

e.g. File name: File.txt cat File.txt Result: #INBOUND_QUEUE=FAQ1 INBOUND_QUEUE=FAQ2 I want to get the value for one which is not commented out. Thanks, (3 Replies)
Discussion started by: Tanu
3 Replies

2. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

3. UNIX for Advanced & Expert Users

Read file and skip the line starting with #

Hi all, I'm new in unix. Need some help here. I have a file called server.cfg which contains the servers name, if I don't want to run on that server, I'll put a "#" infront it. (17 Replies)
Discussion started by: beezy
17 Replies

4. 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

5. Shell Programming and Scripting

replace line starting with not a number

Dear users, I have a file like this: geometry,geometry_vertex_count,Id,strnum,platecode,datatype,dtnum,refnum,appearance,disappeara,color,geogdesc,datatype_ft_style,import_notes "<LineString><coordinates>-130.6539,51.5103,0 -130.7708,51.6287,0 -130.8356,51.6832,0 -130.9211,51.7772,0... (5 Replies)
Discussion started by: Gery
5 Replies

6. Shell Programming and Scripting

Read .txt file and dropping lines starting with #

Hi All, I have a .txt file with some contents as below: Hi How are you? # Fine and you? I want a script file which reads the .txt file and output the lines which does not start with #. Hi How are you? Help is highly appreciated. Please use code tags when posting data and... (5 Replies)
Discussion started by: bghosh
5 Replies

7. UNIX for Dummies Questions & Answers

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: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

9. 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

10. Shell Programming and Scripting

How to get the line number from while read

I have a file TXTPROCESS.TXT.20071129 in which line 1 contains the file name and rest of the records are data. The file data looks like this: TXTPROCESS.TXT.20071129 DIVD20071129 INTR20071129 BALN20071129 From line 2 onwards the first 4 characters defines individual process. What I... (2 Replies)
Discussion started by: skymirror
2 Replies

Featured Tech Videos