0

May be my problem is kinda obvious for you but I really need to get a solution. I need to generate a file.sql file from a file.csv, so I use this command :

cat file.csv |sed "s/\(.*\),\(.*\)/insert into table(value1, value2)
values\('\1','\2'\);/g" > file.sql

It works perfectly, but when the values exceed 9 (for example for \10, \11 etc...) it takes consideration of only the first number (which is \1 in this case) and ignores the rest.

I want to know if I missed something or if there is another way to do it.

Thank you !

EDIT :
The not working example :

My file.csv looks like

2013-04-01 04:00:52,2,37,74,40233964,3860,0,0,4878,174,3,0,0,3598,27.00,27

What I get

insert into table
val1,val2,val3,val4,val5,val6,val7,val8,val9,val10,val11,val12,val13,val14,val15,val16
values ('2013-04-01 07:39:43',2,37,74,36526530,3877,0,0,6080,2013-04-01 07:39:430,2013-04-01 07:39:431,2013-04-01 07:39:432,2013-04-01 07:39:433,2013-04-01 07:39:434,2013-04-01 07:39:435,2013-04-01 07:39:436);

After the ninth element I get the first one instead of the 10th,11th etc...

Imane Fateh
  • 101
  • 2
  • Could you add an example of the data you expect in file.csv, when you mention \10, \11, this would represent anything that you match in sed in escaped brackets \(like this\), of which I only see two, in your script. – Sirch Jun 26 '13 at 11:21
  • @Sirch I added what you asked for, thank you for help :) – Imane Fateh Jun 26 '13 at 11:37
  • If you were using MySQL you could just import the CSV directly, e.g. LOAD DATA INFILE... – Michael Hampton Jun 26 '13 at 14:07
  • Actually, I'm using PostgreSQL and I've just succeeded to generate my .sql file with PHP, but I still wanna know why this method is not working. – Imane Fateh Jun 26 '13 at 14:29
  • 1
    The command that you say you use isnt the command that would generate the stuff you say you get, so its not easy to help. – Sirch Jun 26 '13 at 14:46

1 Answers1

0

You cannot (portably) use more than nine capturing parentheses in sed. You are probably better off writing your script in Perl.

Peter Eisentraut
  • 3,665
  • 1
  • 24
  • 21