[nSLUG] Find and replace command

Jack Warkentin jwark at bellaliant.net
Thu Oct 2 17:54:00 ADT 2014


Hi Trina

Note: long lines so that wraparounds don't muck things up.

sed -i 's/^\(<body\) background="Files/back.jpg" style="background-attachment: fixed; background-repeat: repeat-y;"\(>\)$/\1\2/' <filename>

should do the trick on a single file. Look up the man page for sed (the stream editor) to understand how this example works.

If you set up an alias to this (leaving out the <filename> part) you can then use a find command to do everything in one fell swoop.

For example:

alias FixBody="sed -i 's/^\(<body\) background="Files/back.jpg" style="background-attachment: fixed; background-repeat: repeat-y;"\(>\)$/\1\2/'"

find ... -exec FixBody \{\} \;

You might have to fiddle with the single and double quotes ( ' and " ) in the alias to get it right.

Of course, try it out on a copy of of file first to make sure it works.

Regards

Jack

Trina Conrad wrote:
> Hello all,
>
> I am working on my website and am doing some updates to it and I would like to know is there a way to do a blanket find and replace for multiple files
> so that I do not need to open each file and replace the one line that is needing to be replaced?
>
> I am trying to replace is this line:
>
> <body background="Files/back.jpg" style="background-attachment: fixed; background-repeat: repeat-y;">
>
> With this:
>
> <body>
>
> This needs to be able to sort through multiple folders, sub-folders and even more sub-folders.  :)
>
> Thanks,
> Trina
>
>
> _______________________________________________
> nSLUG mailing list
> nSLUG at nslug.ns.ca
> http://nslug.ns.ca/mailman/listinfo/nslug

-- 
Jack Warkentin, phone 902-404-0457, email jwark at bellaliant.net
39 Inverness Avenue, Halifax, Nova Scotia, Canada, B3P 1X6


More information about the nSLUG mailing list