Read first x lines of csv file into new outfile?

风格不统一 提交于 2019-12-20 11:03:53

问题


How would one copy only the first x lines of a csv file into a new csv file via the terminal?


回答1:


Brief

(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).

Full example

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: 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



回答2:


This might not work for you if your CSV contains "lines" containing newline seperators, e.g. in Quotes. A short PHP Script that would solve this issue, so you would get 1500 "lines"/"datasets", each possibly containing multiple "file lines"

<?php
$input = fopen($argv[1], "r");
$output = fopen($argv[2], "w+");

$limit = $argv[3];
$counter = 0;
while($counter <= $limit) {
    echo $counter;
    $line = fgetcsv($input);
    fputcsv($output, $line);
    $counter++;
} 

To execute:

php -f scriptname.php input.csv output.csv 1500


来源:https://stackoverflow.com/questions/20275072/read-first-x-lines-of-csv-file-into-new-outfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!