Script to find and replace words in multiple files

6

I need to make a command that finds and replace a directory path with 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>

The names of the directories change as well as the .C sources, I tried with the find and sed commands like this:

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

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

I would like the command to return this:

<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>
    
asked by anonymous 15.12.2014 / 17:07

2 answers

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

Why not do it this way? will grab all 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 -regex function to include a regular expression to search for, alias this option can be used to replace the || I used above.

I do not know if I helped, but from what I understood it was something that you wanted.

    
16.12.2014 / 09:57
1

Sed

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

The regular expression A-Za-z0-9/-\_ will correspond to letters and digits, including the characters such as the bar and Bottom Trace . Make sure you are in the correct folder, the above command will search for .html files in the current working folder.

Please note that the above command applies to GNU 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
    
16.12.2014 / 22:59