IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New In file search and replace?
I have a bunch of html files I downloaded to my computer. The problem is they where developed under windows, which doesn't care about slash direction. And the files are full of links like "../help\\drivers/direct.htm" and Linux really doesn't like that.

What is the easiest way to do a search and replace in multiple text files from the command line. I want to just replace all "\\" with "/" for every file in a directory. It might create a few problems but it will fix far more then it will create.

Jay
New I'd do something like this
If it was multiple directories of stuff:

find . -name "*htm" -exec fix.pl {} \\;

Where fix.pl was:

#!/usr/bin/perl -w -pi.old
s{\\\\}{/}g;

Note - it could all be done on a single command line but I rarely use it that way so it doesn't fly off my fingers.
Expand Edited by crazy Dec. 9, 2006, 10:31:01 PM EST
New sed or awk
or Perl which borrowed from them.
   sed "s/\\///g" test.txt >test.copy.txt
New cat filename | tr '\\\\' '/' > tmp; mv tmp filename
Now stick it in a loop like

for n in `find . -name "*html"`
do
cat $n | tr '\\' '/' > tmp; mv tmp $n
done

Those other tools are overkill for this kind of thing.
tr is a much underused utility



[link|http://www.blackbagops.net|Black Bag Operations Log]

[link|http://www.objectiveclips.com|Artificial Intelligence]

[link|http://www.badpage.info/seaside/html|Scrutinizer]
Expand Edited by tuberculosis Dec. 10, 2006, 12:48:10 AM EST
New YAAnswer
Do this in the sh shell:

for file in $( find . -name "*help*.jsp" -print ); do sed -i.bak 's/\\///' $file; done

After you're done making sure that it did what you wanted it to do, go and remove all of your new .bak files.
-YendorMike

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
- Benjamin Franklin, 1759 Historical Review of Pennsylvania
New Hey that's nifty.
I haven't seen that use of for before. I would have written your solution like this:

find . -name "*help*.jsp" -print | ( while read file ; do sed -i.bak 's/\\///' $file; done )

Wade.
"Don't give up!"
New Thanks
Thanks, I had to fix to sed to escape the slashes correctly, but that was simple. It only seemed to correct one instance per line, but I just ran several times.

for file in $( find . -name "*.html" -print ); do sed -i.bak 's|\\|/|' $file; done

Jay
New Just add the "g" modifier to the sed script
That'll get every instance on a line.
-YendorMike

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety."
- Benjamin Franklin, 1759 Historical Review of Pennsylvania
New Late to the party, but have you tried "tidy"?
Link: [link|http://www.w3.org/People/Raggett/tidy/|http://www.w3.org/People/Raggett/tidy/]

It's been a quite a while since I've used it last, but the Microsoft slash problem was one of the things it addressed.

Have you considered the fact that "\\" may legitimately belong in an HTML file outside the context of being a directory delimiter? One can't be too cavalier about replacing one slash with another! :)
Alex

When fascism comes to America, it'll be wrapped in a flag and carrying a cross. -- Sinclair Lewis
     In file search and replace? - (JayMehaffey) - (8)
         I'd do something like this - (crazy)
         sed or awk - (ChrisR)
         cat filename | tr '\\\\' '/' > tmp; mv tmp filename - (tuberculosis)
         YAAnswer - (Yendor) - (3)
             Hey that's nifty. - (static)
             Thanks - (JayMehaffey) - (1)
                 Just add the "g" modifier to the sed script - (Yendor)
         Late to the party, but have you tried "tidy"? - (a6l6e6x)

Hey... Pong. My parents played this game.
109 ms