Find and replace content in 100 lines

zep zgreenfelder at gmail.com
Fri May 1 01:14:14 UTC 2015



On 04/30/2015 08:58 PM, Nancy Belle wrote:
> Hi freebsd-questions
>
> I hope y'all don't mind another question, although I have a slim idea how to do it, but it would need some trial and error to get there. Probably use either sed or perl in some fashion. It's the fashion I'm unsure of.
>
> Running fbsd-9.3
>
> This is probably easy for the script gurus on the list.
>
> Here's the need to fix about 100 lines in a single *.html file:
> find this "../../../arch1/arch14"
> replace with "../../../../../../foo/foo2/foo3/arch1/arch14"
>
> The quotes are there too.
>
> Hope for help, please!
>

normally I'd send jut to the poster, but in the offhand chance this can
help someone else, here are my results:

[zep at nemesis x]$ cat junk
this stuff
is
easy

Here's the need to fix about 100 lines in a single *.html file:
find this "../../../arch1/arch14"
replace with "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
find this "../../../arch1/arch14"
[zep at nemesis x]$ cat try
perl -p -i -e
's?"../../../arch1/arch14"?"../../../../../../foo/foo2/foo3/arch1/arch14"?g'
[zep at nemesis x]$ perl -p -i -e
's?"../../../arch1/arch14"?"../../../../../../foo/foo2/foo3/arch1/arch14"?g'
junk
[zep at nemesis x]$ cat junk
this stuff
is
easy

Here's the need to fix about 100 lines in a single *.html file:
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
replace with "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
find this "../../../../../../foo/foo2/foo3/arch1/arch14"
[zep at nemesis x]$

the "junk" file was just some text to try things with.   to make it
fully functional I'd end with something like:
cd $WEB_DIR
find . -type f (or maybe -name "*.html" if you're sure all files will be
.html) | xargs \
   perl -p -i -e
's?"../../../arch1/arch14"?"../../../../../../foo/foo2/foo3/arch1/arch14"?g'

but I'd make sure to do lots of backups and try first on some junk
files/things you don't really need/are a much smaller subset of files on
a non prod machine.    I just think it'll work, I can't be sure.

oh wait.  you said a single file.   so that could work on just the on
file instead of a find.  (sorry, too lazy to erase all that, I guess) or
you could use vi on the file and do something like
: 1,$
s?"../../../arch1/arch14"?"../../../../../../foo/foo2/foo3/arch1/arch14"?g

-- 
public gpg key id: AE60F64C




More information about the freebsd-questions mailing list