user2090227
user2090227

Reputation: 83

Command Line Find & Replace

I'm trying to replace

</url></loc>

with

</loc></url>

in all my files but nothing seems to be working. Is there a way to do this via command line, perl, etc.? Any help would be greatly appreciated.

Upvotes: 0

Views: 162

Answers (4)

Joy
Joy

Reputation: 320

find . -type f -name "yourname*" -exec sed -i 's/<\/url><\/loc>/<\/loc><\/url>/g' '{}' ';'

Upvotes: 0

William Pursell
William Pursell

Reputation: 212248

Note that many versions of sed do not recognize -i. But you can do exactly the same with perl:

find . -exec perl -i -pe 's|(</url>)(</loc>)|$2$1|g' {} \;

Perl has a big advantage over sed in that it will be much easier to expand this to include occurrences of the two tags that are spread over multiple lines.

perl -0777 -i -pe 's|(</url>)(\s*)(</loc>)|$3$2$1|g' 

Upvotes: 4

speakr
speakr

Reputation: 4209

Use sed:

sed -i 's/<\/url><\/loc>/<\/loc><\/url>/g' yourfiles.*

To replace other tags:

sed -E -i 's/<\/(url|what|ever)><\/(loc|any|other)>/<\/\2><\/\1>/g' yourfiles.*

Upvotes: 1

JaredC
JaredC

Reputation: 5300

Check out sed:

sed -i 's|</url></loc>|</loc></url>|g' <filenames>

Upvotes: 3

Related Questions