Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing a sed -i on a list of files in stdin

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".

like image 651
hobbes3 Avatar asked Oct 28 '25 05:10

hobbes3


1 Answers

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.

like image 135
Explosion Pills Avatar answered Oct 29 '25 19:10

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!