This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/2010-11-04-use-regular-expressions-search-and-replace-coda-or-textmate.md

42 lines
2.1 KiB
Markdown
Raw Normal View History

2015-03-16 21:18:03 +00:00
---
title: Use Regular Expressions to Search and Replace in Coda or TextMate
tags:
2015-06-14 02:27:41 +00:00
- taxonomy
- sequel-pro
- database
- coda
- regular-expression
- textmate
2016-12-29 16:32:52 +00:00
use: [posts]
2015-03-16 21:18:03 +00:00
---
2018-03-01 07:27:33 +00:00
{% block excerpt %}
2015-05-03 17:55:38 +00:00
As in [the original post](/blog/add-taxonomy-term-multiple-nodes-using-sql/ "Quickly adding a taxonomy term to multiple nodes using SQL"), I'd generated a list of node ID values, and needed to add structure the SQL update statment formatted in a certain way. However, I changed my inital query slightly to out put the same nid value twice.
2018-03-01 07:27:33 +00:00
{% endblock %}
2015-03-16 21:18:03 +00:00
2018-03-01 07:27:33 +00:00
{% block content %}
2017-03-16 08:09:52 +00:00
```language-sql
2015-03-16 21:18:03 +00:00
SELECT nid, nid FROM node WHERE TYPE = 'blog' ORDER BY nid ASC;
2017-03-16 08:09:52 +00:00
```
2015-03-16 21:18:03 +00:00
Then, I could select all of the returned rows, copy the values, and paste them into Coda:
As before, I needed my SQL update statement to be in the following format:
2017-03-16 08:09:52 +00:00
```language-sql
2015-03-16 21:18:03 +00:00
INSERT INTO term_node VALUE (nid, vid, tid), (nid2, vid2, tid);
2017-03-16 08:09:52 +00:00
```
2015-03-16 21:18:03 +00:00
As I mentioned previously, the nid and vid values are the same for each node, and the tid will remain constant. In this case, the tid value that I needed to use was '63'.
So, using the 'Find and Replace' function within Coda, combined with [regular expressions](http://en.wikipedia.org/wiki/Regular_expression) (regex), I can easily format the values as needed. To begin with, I need to ensure that the RegEx search option is enabled, and that I'm using the correct escape character.
The first thing that I wanted to do was add the seperating comma between the two values. To do this, I
perform a search for `\s*\t`. This searches for everything that is whitespace AND is a tab value. I can then add the comma as the replacement for each result.
All 31 lines have been changed.
Next, I can use `\n` to target the lines between the rows. I'll replace it with the next comma, the number 63 (the tid value), the closing bracket, another comma, re-add the line and add the opening bracket.
The only two lines that aren't changed are the first and last, as they don't have any line breaks following them. I can complete these lines manually. Now all I need to do is add the beginning of the SQL update statement, then copy and paste it into Sequel Pro.
2018-03-01 07:27:33 +00:00
{% endblock %}