0

I want to filter the csv file line by line and select the lines that satisfy the if condition.

Since the csv file is separated by a comma, so the code should be something like this:

'BEGIN {FS=','}
{while read line 
if (condition) 
   save selected line to a new csv file
} done < file.csv'

How can I save the selected lines to a new csv file if it satisfies the if condition? Can anyone provide some examples?

0

I would use Python for something like this. Here is an example:

import csv

#Create a csv file with some data
myData = [["first_name", "second_name", "Grade"],
          ['Alex', 'Brian', 'A'],
          ['Tom', 'Smith', 'B']]

myFile1 = open('file1.csv', 'w')
with myFile1:
    writer = csv.writer(myFile1)
    writer.writerows(myData)

#Create a second csv file
myFile2 = open('file2.csv', 'w')

#Read the first file created with data
with open('file1.csv') as File:
    reader = csv.reader(File)
    for row in reader:
        #Print every row to the console
        print(row)
        if row[0] == "Alex":
           #If the first cell of the row says Alex, say hi and add the row to the second file
           print "Hi Alex"
           with myFile2:
             writer = csv.writer(myFile2)
             writer.writerow(row)
1

Use awk in this way

awk -F, '(condition) { print >"to_new.csv"}' file.csv

The -F, specify delimiter comma , which the fields are separated. If condition matched with your criteria then it will redirect the line to a new file named to_new.csv.

We used single-‘>’ redirection here. When this type of redirection is used, the to_new.csv is erased before the first output is written to it. Subsequent writes to the same to_new.csv do not erase the file, but append to it. (This is different from how you use redirections in shell scripts.) If to_new.csv does not exist, it is created.

Or simply write:

awk -F, 'condition' file.csv > to_new.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.