[Answered ]-Find a pattern and surround it with quotes

2👍

✅

You just have to set the regular expression to this form: {% url <things> %}. By enclosing groups in parentheses, we can print them back. Note <things> is a set of A:B:C:...:Z, that is, something we define as “anything up to a space”.

All together:

sed 's/\({% url \)\([^ ]*\)\( %}\)/\1"\2"\3/' file

You can do this to all the files in a given folder by saying:

for file in *
do
    sed '...' "$file"
done

If you say sed -i.bak '...' file, you will get the files edited in-place, with a backup file.bak created (it is always good to do so, just in case!).

Test

With your given file stored in a, it returns…

$ sed 's/\({% url \)\([^ ]*\)\( %}\)/\1"\2"\3/' a
{% extends "admin/index.html" %}
{% load i18n %}
{% load url from future %}

{% if not is_popup %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url "admin:index" %}">{% trans 'Home' %}</a>
&rsaquo;
{% for app in app_list %}

Leave a comment