0

We will generate a csv file with below values.

yp1234,577,1,3
yp5678,570,3,5
yp9012,132,8,9

I need to extract data and create files based on second column. If it's value greater than 577 then the whole line has to be extracted and placed in a separate file.

I mean I need a file having lines with second column greater than 577.

  • Please accept the answer if it works. Otherwise, please comment if you require more information. – Sparhawk Jun 25 '14 at 2:30
4

It can be done using the below command.

awk -F "," ' $2 >= 577 ' filename.csv > create_files

You can put the entire contents of the above content into a new file (I have named the new file as create_files) and now you can create new files from this file.

After executing the above command, since, you have not mentioned any filename convention, I have assumed the first column can act as the file name. So, you can follow the approach as,

awk -F "," '{ print $1 }' create_files | while read a; do touch $a; done

I am using the awk command to extract the first column alone to have the file names and based on the first column I create the new files using the while loop and touch command.

However, I assume the first column will always be unique. If it is not unique, we may need to follow another approach. For that you need to edit your question and mention what file name convention you are planning on.

0

One stop shop with awk, assuming column 1 is unique

awk -F "," '$2 >= 577 { print $0 > $1 }' my.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.