How do I form a new string from a parsed CSV line?

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











up vote
0
down vote

favorite












I'm using bash shell. I have a CSV file in which each line's tokens are separated by commas. I want to take the second and third columns and forma new string out of them (an SQL statement). I thought I could use awk for this purpose, so I tried ...



localhost:mydir davea$ awk -F ',' -v OFS=',' "REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv
awk: syntax error at source line 1
context is
REPLACE INTO my_table >>> (ID, <<<
awk: bailing out at source line 1


but as you can see I'm getting an error. Am I leaving something out? How do I form my new string from each line in the CSV file?







share|improve this question




















  • Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
    – Dave
    Dec 6 '17 at 21:30














up vote
0
down vote

favorite












I'm using bash shell. I have a CSV file in which each line's tokens are separated by commas. I want to take the second and third columns and forma new string out of them (an SQL statement). I thought I could use awk for this purpose, so I tried ...



localhost:mydir davea$ awk -F ',' -v OFS=',' "REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv
awk: syntax error at source line 1
context is
REPLACE INTO my_table >>> (ID, <<<
awk: bailing out at source line 1


but as you can see I'm getting an error. Am I leaving something out? How do I form my new string from each line in the CSV file?







share|improve this question




















  • Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
    – Dave
    Dec 6 '17 at 21:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm using bash shell. I have a CSV file in which each line's tokens are separated by commas. I want to take the second and third columns and forma new string out of them (an SQL statement). I thought I could use awk for this purpose, so I tried ...



localhost:mydir davea$ awk -F ',' -v OFS=',' "REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv
awk: syntax error at source line 1
context is
REPLACE INTO my_table >>> (ID, <<<
awk: bailing out at source line 1


but as you can see I'm getting an error. Am I leaving something out? How do I form my new string from each line in the CSV file?







share|improve this question












I'm using bash shell. I have a CSV file in which each line's tokens are separated by commas. I want to take the second and third columns and forma new string out of them (an SQL statement). I thought I could use awk for this purpose, so I tried ...



localhost:mydir davea$ awk -F ',' -v OFS=',' "REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv
awk: syntax error at source line 1
context is
REPLACE INTO my_table >>> (ID, <<<
awk: bailing out at source line 1


but as you can see I'm getting an error. Am I leaving something out? How do I form my new string from each line in the CSV file?









share|improve this question











share|improve this question




share|improve this question










asked Dec 6 '17 at 21:11









Dave

368827




368827











  • Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
    – Dave
    Dec 6 '17 at 21:30
















  • Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
    – Dave
    Dec 6 '17 at 21:30















Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
– Dave
Dec 6 '17 at 21:30




Good catch. But even when I change it to "awk -F ',' -v OFS=',' "print REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('$2', '$2', '$3');" types.csv", I get the same syntax error as above.
– Dave
Dec 6 '17 at 21:30










3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










awk -F ',' '
printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
' types.csv





share|improve this answer





























    up vote
    1
    down vote













    Putting single quotes inside of a single quoted string is really tedious. Here, I'm passing a single quote into the the awk variable "q". I'm also trying to take care of SQL injection.



    awk -F ',' -v q="'" '

    for (i=1; i<=NF; i++) gsub(q, q q, $i)
    print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (" q $1 q "," q $2 q "," q $3 q");"

    ' <<END
    foo,bar,Robert');DROP TABLE students;--
    END




    REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('foo','bar','Robert'');DROP TABLE students;--');





    share|improve this answer




















    • drop table? sounds like a joke
      – Sasha Che
      Dec 6 '17 at 21:45










    • follow the link in my answer...
      – glenn jackman
      Dec 6 '17 at 21:54










    • +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
      – PesaThe
      Dec 6 '17 at 22:02

















    up vote
    0
    down vote













    Might be not the most efficient solution but shall work:



    awk -F, 'print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (x27"$2"x27,x27"$2"x27,x27"$3"x27);"' types.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%2f409314%2fhow-do-i-form-a-new-string-from-a-parsed-csv-line%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      awk -F ',' '
      printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
      ' types.csv





      share|improve this answer


























        up vote
        1
        down vote



        accepted










        awk -F ',' '
        printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
        ' types.csv





        share|improve this answer
























          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          awk -F ',' '
          printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
          ' types.csv





          share|improve this answer














          awk -F ',' '
          printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
          ' types.csv






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 6 '17 at 21:41

























          answered Dec 6 '17 at 21:34









          PesaThe

          47337




          47337






















              up vote
              1
              down vote













              Putting single quotes inside of a single quoted string is really tedious. Here, I'm passing a single quote into the the awk variable "q". I'm also trying to take care of SQL injection.



              awk -F ',' -v q="'" '

              for (i=1; i<=NF; i++) gsub(q, q q, $i)
              print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (" q $1 q "," q $2 q "," q $3 q");"

              ' <<END
              foo,bar,Robert');DROP TABLE students;--
              END




              REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('foo','bar','Robert'');DROP TABLE students;--');





              share|improve this answer




















              • drop table? sounds like a joke
                – Sasha Che
                Dec 6 '17 at 21:45










              • follow the link in my answer...
                – glenn jackman
                Dec 6 '17 at 21:54










              • +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
                – PesaThe
                Dec 6 '17 at 22:02














              up vote
              1
              down vote













              Putting single quotes inside of a single quoted string is really tedious. Here, I'm passing a single quote into the the awk variable "q". I'm also trying to take care of SQL injection.



              awk -F ',' -v q="'" '

              for (i=1; i<=NF; i++) gsub(q, q q, $i)
              print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (" q $1 q "," q $2 q "," q $3 q");"

              ' <<END
              foo,bar,Robert');DROP TABLE students;--
              END




              REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('foo','bar','Robert'');DROP TABLE students;--');





              share|improve this answer




















              • drop table? sounds like a joke
                – Sasha Che
                Dec 6 '17 at 21:45










              • follow the link in my answer...
                – glenn jackman
                Dec 6 '17 at 21:54










              • +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
                – PesaThe
                Dec 6 '17 at 22:02












              up vote
              1
              down vote










              up vote
              1
              down vote









              Putting single quotes inside of a single quoted string is really tedious. Here, I'm passing a single quote into the the awk variable "q". I'm also trying to take care of SQL injection.



              awk -F ',' -v q="'" '

              for (i=1; i<=NF; i++) gsub(q, q q, $i)
              print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (" q $1 q "," q $2 q "," q $3 q");"

              ' <<END
              foo,bar,Robert');DROP TABLE students;--
              END




              REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('foo','bar','Robert'');DROP TABLE students;--');





              share|improve this answer












              Putting single quotes inside of a single quoted string is really tedious. Here, I'm passing a single quote into the the awk variable "q". I'm also trying to take care of SQL injection.



              awk -F ',' -v q="'" '

              for (i=1; i<=NF; i++) gsub(q, q q, $i)
              print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (" q $1 q "," q $2 q "," q $3 q");"

              ' <<END
              foo,bar,Robert');DROP TABLE students;--
              END




              REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('foo','bar','Robert'');DROP TABLE students;--');






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 6 '17 at 21:35









              glenn jackman

              46.8k265103




              46.8k265103











              • drop table? sounds like a joke
                – Sasha Che
                Dec 6 '17 at 21:45










              • follow the link in my answer...
                – glenn jackman
                Dec 6 '17 at 21:54










              • +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
                – PesaThe
                Dec 6 '17 at 22:02
















              • drop table? sounds like a joke
                – Sasha Che
                Dec 6 '17 at 21:45










              • follow the link in my answer...
                – glenn jackman
                Dec 6 '17 at 21:54










              • +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
                – PesaThe
                Dec 6 '17 at 22:02















              drop table? sounds like a joke
              – Sasha Che
              Dec 6 '17 at 21:45




              drop table? sounds like a joke
              – Sasha Che
              Dec 6 '17 at 21:45












              follow the link in my answer...
              – glenn jackman
              Dec 6 '17 at 21:54




              follow the link in my answer...
              – glenn jackman
              Dec 6 '17 at 21:54












              +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
              – PesaThe
              Dec 6 '17 at 22:02




              +1 for for (i=1; i<=NF; i++) gsub(q, q q, $i).
              – PesaThe
              Dec 6 '17 at 22:02










              up vote
              0
              down vote













              Might be not the most efficient solution but shall work:



              awk -F, 'print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (x27"$2"x27,x27"$2"x27,x27"$3"x27);"' types.csv





              share|improve this answer
























                up vote
                0
                down vote













                Might be not the most efficient solution but shall work:



                awk -F, 'print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (x27"$2"x27,x27"$2"x27,x27"$3"x27);"' types.csv





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Might be not the most efficient solution but shall work:



                  awk -F, 'print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (x27"$2"x27,x27"$2"x27,x27"$3"x27);"' types.csv





                  share|improve this answer












                  Might be not the most efficient solution but shall work:



                  awk -F, 'print "REPLACE INTO my_table (ID, NAME, HOURS) VALUES (x27"$2"x27,x27"$2"x27,x27"$3"x27);"' types.csv






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 6 '17 at 21:43









                  Sasha Che

                  1214




                  1214



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409314%2fhow-do-i-form-a-new-string-from-a-parsed-csv-line%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