سؤال

So I recently upgraded Django from 1.4 to 1.5. I found out I need to update all my template codes to add single quotes like from

{% url user_detail pk=user.pk %}

to

{% url 'user_detail' pk=user.pk %}

I'm kind of new to bash commands like sed, but I approached my issue in 2 parts.

The 1st part was to get a list of all the files that needs updating. I did that with

ack "{% url" | cut -d ":" -f1 | uniq

this will give me an ouput like

templates/some_file.html
templates/admin/another_file.html

The 2nd part was playing around with sed

cat templates/some_file.html | sed "s/{% url \([A-Za-z0-9_]*\) /{% url '\1' /g"

Both of those commands work fine. Now the question is

how do I combine those two commands so that sed will add single-quotes around the url name in every file found by ack?

If I try

ack "{% url" | cut -d ":" -f1 | uniq | sed -i "s/{% url \([A-Za-z0-9_]*\) /{% url '\1' /g"

it gives me the error

sed: -i may not be used with stdin

EDIT

I'm running OS X (BSD variant) so I needed to use sed -i "" "s/{% url \([A-Za-z0-9_]*\) /{% url '\1' /g".

هل كانت مفيدة؟

المحلول

ack -l "{% url" | xargs sed -i "s/{% url \([A-Za-z0-9_]*\) /{% url '\1' /g"

-l lists matching files from ack (a simpler version of your cut/uniq). xargs performs the operation on each argument acquired from the pipe (i.e. each file). What you were trying to do would be sed on the actual string of the list of files from stdin which of course can't be updated in place.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top