Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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?

like image 886
ylas Avatar asked Jan 01 '13 11:01

ylas


People also ask

How do I go to the end of a file in bash?

Conclusion. In Linux, to append text to a file, use the >> redirection operator or the tee command.

How do I read a text file line by line in bash?

Syntax: Read file line by line on a Bash Unix & Linux shell file. The -r option passed to read command prevents backslash escapes from being interpreted. Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed. while IFS= read -r line; do COMMAND_on $line; done < input.

How do you read a specific line from a file in Linux?

Using the head and tail Commands First, we get line 1 to X using the head command: head -n X input. Then, we pipe the result from the first step to the tail command to get the last line: head -n X input | tail -1.


1 Answers

tail -n +2 file.csv 

From the man page:

-n, --lines=N      output the last N lines, instead of the last 10 ...  If the first character of N (the number of bytes or lines)  is  a  '+', print  beginning with the Nth item from the start of each file, other- wise, print the last N items in the file. 

In English this means that:

tail -n 100 prints the last 100 lines

tail -n +100 prints all lines starting from line 100

like image 69
Martin Avatar answered Sep 20 '22 18:09

Martin