Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2011
    Posts
    2

    Question 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?

  2. #2
    Just Joined! Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    50
    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...)

  3. #3
    Linux Newbie
    Join Date
    Dec 2010
    Posts
    146
    You should try Google translate to make the message clearer.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2011
    Location
    Patagonia argentina
    Posts
    9
    Quote Originally Posted by Rava View Post
    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...)
    Should be:

    cat $webfile | sed -e 's/_files//g' > $webfile-out

    /EDIT: :/ ...uh, your line works too, should have tried it before correcting. Sorry for the noise
    Last edited by mendieta; 02-20-2011 at 03:22 AM.

  6. #5
    Just Joined! Rava's Avatar
    Join Date
    Jul 2007
    Location
    hacking 127.0.0.1
    Posts
    50

    Talking

    Quote Originally Posted by dE_logics View Post
    You should try Google translate to make the message clearer.
    Do you refer to me or the OT?

    Quote Originally Posted by mendieta View Post
    /EDIT: :/ ...uh, your line works too, should have tried it before correcting. Sorry for the noise
    Cool! Seems my memory is still working okayish....

  7. #6
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    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:
    Code:
    #!/bin/sh
    mv -t $TARGET_DIRECTORY $1
    In this code, the first input parameter "$1" will receive one file path for each found item from the "find" command.

    Then execute this on the command line:
    Code:
    find $IN_THIS_FOLDER -type d -name \*_files -execdir $HOME/my_rename.sh '{}' \;
    This will execute "my_rename.sh" for every directory that ends with the string "_files".

    Before executing "find" don't forget to make the script have executable permissions:
    Code:
    chmod ug+x $HOME/my_rename.sh
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •