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?
Conclusion. In Linux, to append text to a file, use the >> redirection operator or the tee command.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With