<p>I'm importing some data from a <code>CSV</code> file, and numbers that are larger than <code>1000</code> get turned into <code>1,100</code> etc. </p>

<p>What's a good way to remove both the quotes and the comma from this so I can put it into an <code>int</code> field?</p>

<p><strong>Edit:</strong> </p>

<p>The data is actually already in a MySQL table, so I need to be able to this using SQL. Sorry for the mixup.</p>

[{'answer_id': 7934, 'author': 'Eldila', 'author_id': 889, 'author_profile': 'https://Stackoverflow.com/users/889', 'pm_score': 0, 'selected': False, 'text': '<p>You could use this perl command.</p>\n\n<pre><code>Perl -lne \'s/[,|"]//; print\' file.txt &gt; newfile.txt\n</code></pre>\n\n<p>You may need to play around with it a bit, but it should do the trick.</p>\n'}, {'answer_id': 7944, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s the PHP way:</p>\n\n<pre><code>$stripped = str_replace(array(\',\', \'"\'), \'\', $value);\n</code></pre>\n\n<p><a href="http://w3schools.com/php/func_string_str_replace.asp" rel="nofollow noreferrer">Link to W3Schools page</a></p>\n'}, {'answer_id': 7962, 'author': 'BlaM', 'author_id': 999, 'author_profile': 'https://Stackoverflow.com/users/999', 'pm_score': -1, 'selected': False, 'text': '<p>Daniel\'s and Eldila\'s answer have one problem: They remove all quotes and commas in the whole file.</p>\n\n<p>What I usually do when I have to do something like this is to first replace all separating quotes and (usually) semicolons by tabs. </p>\n\n<ul>\n<li><em>Search:</em> ";"</li>\n<li><em>Replace:</em> \\t</li>\n</ul>\n\n<p>Since I know in which column my affected values will be I then do another search and replace:</p>\n\n<ul>\n<li><em>Search:</em> ^([\\t]+)\\t([\\t]+)\\t([0-9]+),([0-9]+)\\t</li>\n<li><em>Replace:</em> \\1\\t\\2\\t\\3\\4\\t</li>\n</ul>\n\n<p>... given the value with the comma is in the third column.</p>\n\n<p>You need to start with an "^" to make sure that it starts at the beginning of a line. Then you repeat ([0-9]+)\\t as often as there are columns that you just want to leave as they are.</p>\n\n<p>([0-9]+),([0-9]+) searches for values where there is a number, then a comma and then another number.</p>\n\n<p>In the replace string we use \\1 and \\2 to just keep the values from the edited line, separating them with \\t (tab). Then we put \\3\\4 (no tab between) to put the two components of the number without the comma right after each other. All values after that will be left alone.</p>\n\n<p>If you need your file to have semicolon to separate the elements, you then can go on and replace the tabs with semicolons. However then - if you leave out the quotes - you\'ll have to make sure that the text values do not contain any semicolons themselves. That\'s why I prefer to use TAB as column separator.</p>\n\n<p>I usually do that in an ordinary text editor (EditPlus) that supports RegExp, but the same regexps can be used in any programming language.</p>\n'}, {'answer_id': 8001, 'author': 'Eldila', 'author_id': 889, 'author_profile': 'https://Stackoverflow.com/users/889', 'pm_score': 0, 'selected': False, 'text': '<p>My command does remove all \',\' and \'"\'.</p>\n\n<p>In order to convert the sting "1,000" more strictly, you will need the following command.</p>\n\n<pre><code>Perl -lne \'s/"(\\d+),(\\d+)"/$1$2/; print\' file.txt &gt; newfile.txt\n</code></pre>\n'}, {'answer_id': 8059, 'author': 'Eldila', 'author_id': 889, 'author_profile': 'https://Stackoverflow.com/users/889', 'pm_score': 0, 'selected': False, 'text': '<p>Actually nlucaroni, your case isn\'t quite right. Your example doesn\'t include double-quotes, so</p>\n\n<pre><code>id,age,name,...\n1,23,phil,\n</code></pre>\n\n<p>won\'t match my regex. It requires the format "XXX,XXX". I can\'t think of an example of when it will match incorrectly.</p>\n\n<p>All the following example won\'t include the deliminator in the regex:</p>\n\n<blockquote>\n<pre><code>"111,111",234\n234,"111,111"\n"111,111","111,111"\n</code></pre>\n</blockquote>\n\n<p>Please let me know if you can think of a counter-example.</p>\n\n<p>Cheers!</p>\n'}, {'answer_id': 8113, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 3, 'selected': True, 'text': '<p>Here is a good case for regular expressions.  You can run a find and replace on the data either before you import (easier) or later on if the SQL import accepted those characters (not nearly as easy).  But in either case, you have any number of methods to do a find and replace, be it editors, scripting languages, GUI programs, etc.  Remember that you\'re going to want to find and replace <em>all</em> of the bad characters.</p>\n\n<p>A typical regular expression to find the comma and quotes (assuming just double quotes) is: <em>(Blacklist)</em></p>\n\n<pre><code>/[,"]/\n</code></pre>\n\n<p>Or, if you find something might change in the future, this regular expression, matches anything except a number or decimal point. <em>(Whitelist)</em></p>\n\n<pre><code>/[^0-9\\.]/\n</code></pre>\n\n<p>What has been discussed by the people above is that we don\'t know all of the data in your CSV file.  It sounds like you want to remove the commas and quotes from all of the numbers in the CSV file.  But because we don\'t know what else is in the CSV file we want to make sure that we don\'t corrupt other data.  Just blindly doing a find/replace could affect other portions of the file.</p>\n'}, {'answer_id': 8135, 'author': 'Eldila', 'author_id': 889, 'author_profile': 'https://Stackoverflow.com/users/889', 'pm_score': 0, 'selected': False, 'text': '<p>The solution to the changed question is basically the same.</p>\n\n<p>You will have to run select query with the regex where clause.</p>\n\n<p>Somthing like</p>\n\n<pre><code>Select *\n  FROM SOMETABLE\n  WHERE SOMEFIELD REGEXP \'"(\\d+),(\\d+)"\'\n</code></pre>\n\n<p>Foreach of these rows, you want to do the following regex substitution s/"(\\d+),(\\d+)"/$1$2/ and then update the field with the new value.</p>\n\n<p>Please Joseph Pecoraro seriously and have a backup before doing mass changes to any files or databases. Because whenever you do regex, you can seriously mess up data if there are cases that you have missed.</p>\n'}, {'answer_id': 8193, 'author': 'Joseph Pecoraro', 'author_id': 792, 'author_profile': 'https://Stackoverflow.com/users/792', 'pm_score': 4, 'selected': False, 'text': '<p>My guess here is that because the data was able to import that the field is actually a varchar  or some character field, because importing to a numeric field might have failed.  Here was a test case I ran purely a MySQL, SQL solution.</p>\n\n<ol>\n<li><p>The table is just a single column (alpha) that is a varchar.</p>\n\n<pre><code>mysql&gt; desc t;\n\n+-------+-------------+------+-----+---------+-------+\n| Field | Type        | Null | Key | Default | Extra |\n+-------+-------------+------+-----+---------+-------+\n| alpha | varchar(15) | YES  |     | NULL    |       | \n+-------+-------------+------+-----+---------+-------+\n</code></pre></li>\n<li><p>Add a record</p>\n\n<pre><code>mysql&gt; insert into t values(\'"1,000,000"\');\nQuery OK, 1 row affected (0.00 sec)\n\nmysql&gt; select * from t;\n\n+-------------+\n| alpha       |\n+-------------+\n| "1,000,000" | \n+-------------+\n</code></pre></li>\n<li><p>Update statement.</p>\n\n<pre><code>mysql&gt; update t set alpha = replace( replace(alpha, \',\', \'\'), \'"\', \'\' );\nQuery OK, 1 row affected (0.00 sec)\nRows matched: 1  Changed: 1  Warnings: 0\n\nmysql&gt; select * from t;\n\n+---------+\n| alpha   |\n+---------+\n| 1000000 | \n+---------+\n</code></pre></li>\n</ol>\n\n<p>So in the end the statement I used was:</p>\n\n<pre><code>UPDATE table\n   SET field_name = replace( replace(field_name, \',\', \'\'), \'"\', \'\' );\n</code></pre>\n\n<p>I looked at the <a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace" rel="nofollow noreferrer">MySQL Documentation</a> and it didn\'t look like I could do the regular expressions find <em>and replace</em>.  Although you could, like <a href="https://stackoverflow.com/questions/7917/remove-quotes-and-commas-from-a-string-in-mysql#8135">Eldila</a>, use a regular expression for a find and then an alternative solution for replace.</p>\n\n<hr>\n\n<p>Also be careful with <code>s/"(\\d+),(\\d+)"/$1$2/</code> because what if the number has more then just a single comma, for instance "1,000,000" you\'re going to want to do a global replace (in perl that is <code>s///g</code>). But even with a global replace the replacement starts where you last left off (unless perl is different), and would miss the every other comma separated group.  A possible solution would be to make the first (\\d+) optional like so <code>s/(\\d+)?,(\\d+)/$1$2/g</code> and in this case I would need a second find and replace to strip the quotes.</p>\n\n<p>Here are some ruby examples of the regular expressions acting on just the string "1,000,000", notice there are NOT double quote inside the string, this is just a string of the number itself.</p>\n\n<pre><code>&gt;&gt; "1,000,000".sub( /(\\d+),(\\d+)/, \'\\1\\2\' )\n# =&gt; "1000,000"  \n&gt;&gt; "1,000,000".gsub( /(\\d+),(\\d+)/, \'\\1\\2\' )\n# =&gt; "1000,000"  \n&gt;&gt; "1,000,000".gsub( /(\\d+)?,(\\d+)/, \'\\1\\2\' )\n# =&gt; "1000000"  \n&gt;&gt; "1,000,000".gsub( /[,"]/, \'\' )\n# =&gt; "1000000"  \n&gt;&gt; "1,000,000".gsub( /[^0-9]/, \'\' )\n# =&gt; "1000000"\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/7917', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/402/']