Alex Rivera | Logout

how to read file from line x to the end of a file in bash

Asked 2013-01-01T11:58:38.910
65

I would like know how I can read each line of a csv file from the second line to the end of file in a bash script.

I know how to read a file in bash:

while read line
 do   
   echo -e "$line\n"
done < file.csv

But, I want to read the file starting from the second line to the end of the file. How can I achieve this?

Edit
Report

1 Answer

1

I would just get a variable.

#!/bin/bash

i=0
while read line
do
    if [ $i != 0 ]; then
      echo -e $line
    fi
    i=$i+1
done < "file.csv"

UPDATE Above will check for the $i variable on every line of csv. So if you have got very large csv file of millions of line it will eat significant amount of CPU cycles, no good for Mother nature.

Following one liner can be used to delete the very first line of CSV file using sed and then output the remaining file to while loop.

sed 1d file.csv | while read d; do echo $d; done
answered 2013-01-01T12:17:47.787

Your Answer