filter lines in csv and save to a new csv file
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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?
text-processing csv
add a comment |Â
up vote
0
down vote
favorite
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?
text-processing csv
1
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
text-processing csv
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?
text-processing csv
edited May 8 at 16:54
ñÃÂsýù÷
14.8k82462
14.8k82462
asked May 8 at 13:35
zz_0513
103
103
1
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05
add a comment |Â
1
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05
1
1
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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)
add a comment |Â
up vote
1
down vote
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
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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)
add a comment |Â
up vote
0
down vote
accepted
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)
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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)
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)
answered May 8 at 16:17
Katu
50828
50828
add a comment |Â
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
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
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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
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
edited May 8 at 17:01
answered May 8 at 16:44
ñÃÂsýù÷
14.8k82462
14.8k82462
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442545%2ffilter-lines-in-csv-and-save-to-a-new-csv-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
1) post a testable input fragment; 2) specify the crucial condition
â RomanPerekhrest
May 8 at 13:41
Start with the awk tag info page on Stack Overflow: stackoverflow.com/tags/awk/info
â glenn jackman
May 8 at 15:09
4/22/2018,1:00:39, 37.7875
â zz_0513
May 8 at 19:05