How would one copy only the first x lines of a csv file into a new csv file via the terminal?
(You'll use a linux terminal/console)
Use head -n NUMBEROFLINES file.csv to get the first NUMBEROFLINES of lines. Write it into another file using shell redirection (>) like this:
head -n NUMBEROFLINES file.csv > mynewfile.csv Note that this will totally recreate mynewfile.csv, if it had any content before it is now deleted forever(-ish).
If you ever happen to want the opposite (last x lines), use tail.
Both tools come with man and info pages (man head or info head - get used to man, though) and a --help flag (head --help actually shows me more or less the man page).
head -n 10 data.csv >> /tmp/first_and_last.csv # Note the ">>" tail -n 10 data.csv >> /tmp/first_and_last.csv # Note the ">>" This would open the file /tmp/first_and_last.csv and attach (>>, > would recreate/delete the file!) the first and the last 10 lines of data.csv at the "end" of /tmp/first_and_last.csv.
Mac OS X: According to the internet (tm) these commands are available in (Unix-based) Mac OS as well (you have to start the Terminal via Finder).
More speaking examples
-n is short for --lines=, so you could also use:
tail --lines=10 data.csv >> addtothisfile.txt head --lines=10 data.csv >> addtothisfile.txt
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