Script to find and replace words in multiple files

Asked

Viewed 623 times

7

I need to make a command that finds and replaces a directory path by another path, in several HTML files, example of part of the HTML file:

<DIV STYLE="margin-top:6pt;margin-left:36pt;" >< FONT ID="f16" >
<A HREF="file:/export100/DOCS_AB/export200/ab.c" >
file:/export100/DOCS_AB/export200/ab.c</A></FONT></DIV>

<DIV STYLE="margin-top:6pt;margin-left:36pt;" >< FONT ID="f16">
<A HREF="file:/export100/DOCS_CD/export200/cd.c">
file:/export100/DOCS_CD/export200/cd.c</A></FONT></DIV>

Directory names change as well as sources .C, I tried with the commands find and sed thus:

find ./ -name *.html | xargs sed -i 's_/export_/media/pendrive_g'

The problem is that directories and sources always change the name.

I wish command would return this to me:

<DIV STYLE="margin-top:6pt;margin-left:36pt;"><FONT ID="f16">
<A HREF="file:/media/pendrive/"nome_arquivo".c">
file:/media/pendrive/"nome_arquivo".c</A></FONT></DIV>
  • 1

    The tag <FONT></FONT> fell into disuse a long time ago. can include this in attribute style of div.

  • An initial idea to make this work, but that is extremely bad to be a final solution, would be to take the direct path to the file. Another idea would be the use of softlinks.

  • The problem is that I can’t modify the html file, the only thing I can and want to do is change the directory names to point to the USB stick.

  • script I don’t know anymore has a little program that you do it very quickly and easily . You will only have the trouble of downloading and upar follow the TEXTREP download link Download TEXTREP

2 answers

4

find ./* -name *.* | xargs sed -i 's_/export_/media/pendrive_g'

Why not do it this way? will get all the files that have some kind of extension. Another way is to use || for it to look for a valid situation and if it finds it executes the code.

find ./* -name *.html | xargs sed -i 's_/export_/media/pendrive_g' || find ./* -name *.c | xargs sed -i 's_/export_/media/pendrive_g'

In the find command you have the option -maxdepth and -mindepth that defines the maximum and minimum level of directories to be searched as well as the function -regex to include a regular expression to be searched for, alias this option can be used to replace the || that I used above.

I don’t know if I helped, but from what I understand it was something you wanted.

1

Sed

sed -i 's#export\([A-Za-z0-9/-\_]*\)/#media/pendrive/#g' *.html

To regular expression A-Za-z0-9/-\_ will match letters and digits, including characters such as bar and lower dash. Make sure you are in the right folder, the above command will search for files .html in the current workbook.

Remember that the above command applies to WILDEBEEST sed, may not work as expected if you are using Freebsd for example.-


Perl

perl -i -p -e 's#export([\w-\/]+)\/#media\/pendrive/#g;' *.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.