How to start reading from the nth line till the last line of a file.

Hi,
For my reuirement, I have to read a file from the 2nd line till the last line<EOF>.

Say,

I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines.
 
for i in `cat test.txt`
{
echo $i
}
 
While doing the above loop, I have read from the 2nd line till last line<EOF> skipping the 1st line (header)

Can someone please help in this?

Maybe there is an easier/better/efficient way to do this. For the moment, try this:

grep -v `head -1 /tmp/test` /tmp/test

Here is the output:

my /tmp/test has the following lines:

Thanks!

Much faster with "sed". Also by using "while read" rather than "for" we preserve each record intact:

sed -n '2,$ p' test.txt | while read line
do
         echo "${line}"
done

Note the double quotes round the string variable $line.

1 Like

or awk:

awk 'NR>8' file | while read LINE
do
        ...
done

Also see useless use of cat and useless use of backticks. You should NOT be reading files by `cat`, that has many problems and corners.

1 Like

Guys Thanks a lot!! Will no more use cat...