Skip to main content

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 (<60) and the second for patients over 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).

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 (<60) and the second for patients over 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 

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 (<60) and the second for patients over 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).

Became Hot Network Question
retagged; corrected spelling
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

Hi iI am using Ubuntu and I want to split my csv file tointo two csv files based on the value in the second column value (age) first. The first file for patients under 60 (<60) and the second for patients over 60(>=) for. For example, if iI 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 

iI have tried the following code but it removes the header ( columns namecolumn names) how should ican I avoid this  ?

awk -F ',' '($2>=60){print}' file.csv > file_over.csv

awk -F ',' '($2>=60){print}' file.csv > file_over.csv 

Hi i want to split my csv file to two csv files based on second column value (age) first file for patients under 60 (<60) and the second for patients over 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 ( columns name) how should i avoid this  ?

awk -F ',' '($2>=60){print}' file.csv > file_over.csv

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 (<60) and the second for patients over 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 
Source Link

Split a CSV file based on second column value

Hi i want to split my csv file to two csv files based on second column value (age) first file for patients under 60 (<60) and the second for patients over 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 ( columns name) how should i avoid this ?

awk -F ',' '($2>=60){print}' file.csv > file_over.csv