Koohoolinn
Koohoolinn

Reputation: 1497

Why does this sed regex substitution not work?

I have a file with id's for which I want to create db INSERT's.
The SQL I want to use: INSERT INTO docstore.migratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), 'id_from_file', now(), 'HERC-742');

I'm using sed with regex but the substitution seems to mess up the result.

File with id's:

$ cat object_ids_01.sql 
09ab41308002760e                                                
09ab41308002760f                                                
09ab413080027610                                                
09ab413080027611                                                
...

When I run this command:
$ sed -E "s/(.*)/INSERT INTO docstore.migratie_dms \(id, document_id, creatiedatum, gebruiker\) VALUES \(uuid_generate_v4\(\), \'\1\', now\(\), \'HERC-742\'\);/" object_ids_01.sql > output.sql

I get these results:

$ cat output.sql 
', now(), 'HERC-742');igratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), '09ab41308002760e                                                
', now(), 'HERC-742');igratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), '09ab41308002760f                                                
', now(), 'HERC-742');igratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), '09ab413080027610                                                
', now(), 'HERC-742');igratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), '09ab413080027611
...

It seems the \1 is at fault because when I replace it with a constant string I get decent INSERT's:

$ sed -E "s/(.*)/INSERT INTO docstore.migratie_dms \(id, document_id, creatiedatum, gebruiker\) VALUES \(uuid_generate_v4\(\), \'xxxxxx\', now\(\), \'HERC-742\'\);/" object_ids_01.sql > output.sql
$ cat output.sql 
INSERT INTO docstore.migratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), 'xxxxxx', now(), 'HERC-742');
INSERT INTO docstore.migratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), 'xxxxxx', now(), 'HERC-742');
INSERT INTO docstore.migratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), 'xxxxxx', now(), 'HERC-742');
INSERT INTO docstore.migratie_dms (id, document_id, creatiedatum, gebruiker) VALUES (uuid_generate_v4(), 'xxxxxx', now(), 'HERC-742');
... 

What am I doing wrong?

Upvotes: 0

Views: 48

Answers (1)

automaton
automaton

Reputation: 1121

Your input probably has trailing white space which (.*) is picking. ([^[:space:]]*) seems to work.

Upvotes: 1

Related Questions