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!

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
Hostname
Value1=abc
Value2=def
Value3=xyz
Hostname1
Value1=abc1
Value2=def1
Value3=xyz1
Hostname2
Value1=abc2
Value2=def2
Value3=xyz2
 

We want to read from 5th line out of 12 lines in input file. Then following is the script for same.

 cat test.ksh
 number=5
 awk -vNUM=`echo $number` '(NR>=NUM)' test1
  
 OR
  
 cat test.ksh
 awk -vNUM=5 '(NR>=NUM)' test1
 

Output will be as follows.

 Hostname1
Value1=abc1
Value2=def1
Value3=xyz1
Hostname2
Value1=abc2
Value2=def2
Value3=xyz2
 

Hope this helps.

Thanks,
R. Singh

Really depends on what you plan to do next.

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

It depends what you mean by "...read 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
do
    ..... # Whatever you need
done

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 
do
    ..... # Whatever you need
done

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?

Robin

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.

#!/bin/bash
# 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.
ifs_str="$IFS"
IFS=$'\n'
# Working part.
n=1
while read -r line
do
	if [ $n -ge $1 ] && [ $n -le $2 ]
	then
		echo -E $line
	fi
	n=$(($n+1))
done < $3 > $4
# End of working part.
cat $4
IFS="$ifs_str"
exit

Results...

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 ~/AudioScope.sh /tmp/text
#!/bin/bash
AMIGA:barrywalker~/Desktop/Code/Shell> ./line_no 3470 3489 ~/AudioScope.sh /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> _

EDIT:
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.