How do I form a new string from a parsed CSV line?
Clash 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?
bash shell-script awk csv
add a comment |Â
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?
bash shell-script awk csv
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
add a comment |Â
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?
bash shell-script awk csv
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?
bash shell-script awk csv
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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;--');
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 forfor (i=1; i<=NF; i++) gsub(q, q q, $i)
.
â PesaThe
Dec 6 '17 at 22:02
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
awk -F ',' '
printf("REPLACE INTO my_table (ID, NAME, HOURS) VALUES ('''%s''', '''%s''', '''%s''');n", $2, $2, $3)
' types.csv
edited Dec 6 '17 at 21:41
answered Dec 6 '17 at 21:34
PesaThe
47337
47337
add a comment |Â
add a comment |Â
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;--');
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 forfor (i=1; i<=NF; i++) gsub(q, q q, $i)
.
â PesaThe
Dec 6 '17 at 22:02
add a comment |Â
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;--');
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 forfor (i=1; i<=NF; i++) gsub(q, q q, $i)
.
â PesaThe
Dec 6 '17 at 22:02
add a comment |Â
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;--');
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;--');
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 forfor (i=1; i<=NF; i++) gsub(q, q q, $i)
.
â PesaThe
Dec 6 '17 at 22:02
add a comment |Â
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 forfor (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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Dec 6 '17 at 21:43
Sasha Che
1214
1214
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%2f409314%2fhow-do-i-form-a-new-string-from-a-parsed-csv-line%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
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