2

I currently have an SQL file filled with insert statements in the form:

 INSERT INTO
 database.table(primary_key,
 foreign_key, url_one,
 url_two, int_one,
 int_two, int_three) VALUES
 (266,7,https://some.url.com/image_one.png,https://some.url.com/image_two.png,266,1440,900);

I've now realised that the URL strings will need quotation marks around them before I can execute them in MySQL. What command can I use to place quotation marks around the URL string?

I tried to use 'sed' to do it but I couldn't get the command right as I don't completely know the syntax of what I want to achieve:

sed -e "s/(https://).*(\.png)/\"(https://)$1(.png)\"/g" some_sql_file.sql

Running this results in:

sed: -e expression #1, char 12:

unknown option to `s'

Any help would be much appreciated as I want to avoid regenerating the SQL file just to put in the quotation marks if at all possible.

  • Remember. Manpages are your friend! :-) – nanofarad Jul 18 '12 at 19:18
2

I would use:

sed -e 's@\(https\?://[^,]*\),@"\1",@g' FILE

This looks for a expression starting with http and an optional s followed by ://. Then it takes everything up to the first ,. This expression will be enclosed by ".

| improve this answer | |
  • Thanks for that! How would I get it to write the changes back into the file? – Ross Sep 19 '10 at 22:00
  • @Rosco: Use the -i option to sed. – Paused until further notice. Sep 19 '10 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.