LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Remove every fourth line from CSV file? (https://www.linuxquestions.org/questions/linux-newbie-8/remove-every-fourth-line-from-csv-file-747002/)

briana.paige 08-12-2009 09:40 AM

Remove every fourth line from CSV file?
 
Hi,

I have a large csv file which has data for every 15 minutes and I need only the hourly data. I would like to keep the first line, delete the next three lines, keep the fourth etc...

Sample of the orig data:

Code:

01/01/2009        0:00:57        sleep        84        8.4        -13.11
01/01/2009        0:16:12        sleep        84        8.4        -13.11
01/01/2009        0:31:27        sleep        84        8.4        -13.11
01/01/2009        0:46:42        sleep        84        8.4        -13.11
01/01/2009        1:01:57        sleep        84        8.4        -13.11
01/01/2009        1:17:12        sleep        84        8.4        -13.11
01/01/2009        1:32:27        sleep        84        8.4        -13.11
01/01/2009        1:47:42        sleep        81        8.1        -13.28
01/01/2009        2:02:57        sleep        81        8.1        -13.28

Desired output from above data:

Code:

01/01/2009        0:00:57        sleep        84        8.4        -13.11
01/01/2009        1:01:57        sleep        84        8.4        -13.11
01/01/2009        2:02:57        sleep        81        8.1        -13.28

A simple awk or bash command would suffice as I only need to perform this operation on 1 or 2 files.

Any help is appreciated!
Thanks.

pwc101 08-12-2009 09:49 AM

Code:

sed -n '1~4p' input
Prints every 4th line. Add -i to make the changes permanent.

See http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html

briana.paige 08-12-2009 10:06 AM

Works like a charm, exactly what I needed! Thank-you so much!

pwc101 08-12-2009 10:07 AM

No problem :)

If you want to change the starting line, change the 1 to the line you want to start from. Likewise change 4 to 8 if you want it every 2 hours instead.

I'm sure there is an awk solution too, but if this works, so much the better. It's not exactly the longest command ever, either :)


All times are GMT -5. The time now is 10:52 AM.