Results 1 to 6 of 6
I want to write Bourne-shell script that will be to do finding and replacement in any web page code (.htm file) name of the tied folder in which have been ...
- 02-19-2011 #1Just Joined!
- Join Date
- Feb 2011
- Posts
- 2
bash script for finding and replacement in any web page code
I want to write Bourne-shell script that will be to do finding and replacement in any web page code (.htm file) name of the tied folder in which have been saved pictures, .css, .js and other files.
This folder create a web browser when we save web page completely and has so name as web page and has ending '_files'.
I have many web pages where name of their folder are incorrect. Of course, my web browser shows these web pages without pictures.
I can count amount of web pages in a folder (/path) needed for me.
1) find /path -type f -name "*.htm*" -print | grep -c .htm or
find /path -type f -name *.htm | wc -l
I can get list of web pages.
2) ls /path *.htm > out-list
But I don't know how to assign the value from out-list (2) or result commands from pipeline (1) to a variable.
HOW TO DO THIS????
Then I want to do next:
3)
var="1"
# where variable 'list' is an amount of web pages
while [ $var -le $list ]
do
4) …......
8 …......
var=$[$var+1]
done
4) assign the 1st (then 2nd , etc. ) value from out-list (2) to variable 'webfile'
sed -n $var,+0p out-spisok
5) find the 1st string value '_files' in the 'webfile'
grep -m1 _files $webfile
6) For example, 'abracadabra_files' is an incorrect folder in the 'webfile'
I must to know start and end position 'abracadabra' without ending '_files', “cut” name of the incorrect folder and assign it to the variable 'finder'
finder = 'abracadabra'
BTW, name of a folder before '_files' is between '=”' and '_files' in any web page code.
HOW TO DO THIS????
7) foldernew = $webfile (without '.htm')
' foldernew' is equal with name of the tied folder without ending '_files' in the folder '/path'
8 find and replace in the 'webfile' and save result in the 'webfile-out'
sed s/$finder/$foldernew/g $webfile > $webfile-out
Can anybody help me?
- 02-19-2011 #2
Seems the only think you want to be erased is the string "_files"...
That could be done with sed, like
sed s/_files//g $webfile > $webfile-out
(when I am not mistaken, have not used sed in a long time...)
- 02-20-2011 #3Linux Newbie
- Join Date
- Dec 2010
- Posts
- 146
You should try Google translate to make the message clearer.
- 02-20-2011 #4Just Joined!
- Join Date
- Feb 2011
- Location
- Patagonia argentina
- Posts
- 9
- 02-20-2011 #5
- 02-21-2011 #6Linux Newbie
- Join Date
- Nov 2008
- Location
- Tokyo, Japan
- Posts
- 243
You are using Firefox to save a web page, and in the "save" dialog, you select "Complete Web Page", and you want to store images and style sheets in a central location rather than in a corresponding "_files" directory, am I correct?
The reason Firefox creates a separate directory for each saved page is that often different sites use different stylesheets but use the same filename. For examples "red.com" will create a "styles.css" and "blue.com" will create a "style.css", but both style sheets will be different. It is better to save web pages using "wget -p" to solve this problem.
If you absolutely must use bourne shell...
You cannot "assign" to values output from a "grep" pipeline because values are just strings, not actual file handles.
The "find" command actually allows you to execute a script for each file it finds. If you create a file called "my_rename.sh" in your $HOME directory which contains this code:In this code, the first input parameter "$1" will receive one file path for each found item from the "find" command.Code:#!/bin/sh mv -t $TARGET_DIRECTORY $1
Then execute this on the command line:This will execute "my_rename.sh" for every directory that ends with the string "_files".Code:find $IN_THIS_FOLDER -type d -name \*_files -execdir $HOME/my_rename.sh '{}' \;
Before executing "find" don't forget to make the script have executable permissions:You can of course change the "my_rename.sh" to do anything you wish, including executing a "sed" script to replace all references to the files in "*_files" in every relevant "*.html" file.Code:chmod ug+x $HOME/my_rename.sh


Reply With Quote

