Sample Header Ad - 728x90

Split a CSV file based on second column value

7 votes
6 answers
1491 views
I am using Ubuntu and I want to split my csv file into two csv files based on the value in the second column (age). The first file for patients under 60 (=). For example, if I have the following input:
id,age
1,65
2,63
3,5
4,55
5,78
The desired output is: file_under:
id,age
3,5
4,55
file_over:
id,age
1,65
2,63
5,78
I have tried the following code but it removes the header (column names) how can I avoid this?
awk -F ',' '($2>=60){print}' file.csv > file_over.csv
The input file is about 50k rows (lines).
Asked by Solomon123 (131 rep)
Apr 5, 2023, 12:37 PM
Last activity: Sep 27, 2023, 04:41 PM