filter lines in csv and save to a new csv file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite
1












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?







share|improve this question

















  • 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














up vote
0
down vote

favorite
1












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?







share|improve this question

















  • 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












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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?







share|improve this question













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?









share|improve this question












share|improve this question




share|improve this question








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












  • 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










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)





share|improve this answer




























    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





    share|improve this answer























      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      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






























      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)





      share|improve this answer

























        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)





        share|improve this answer























          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)





          share|improve this answer













          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)






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 8 at 16:17









          Katu

          50828




          50828






















              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





              share|improve this answer



























                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





                share|improve this answer

























                  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





                  share|improve this answer















                  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






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited May 8 at 17:01


























                  answered May 8 at 16:44









                  αғsнιη

                  14.8k82462




                  14.8k82462






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      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













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay