Results 1 to 5 of 5

Thread: sed - How to use the matched result

  1. #1
    Join Date
    Nov 2013
    Beans
    45

    sed - How to use the matched result

    I'm making a script that will automatically start up (i.e. make the folders and files and stuff) websites for me, but the syntax is killing me. Heres the script:
    Code:
    #!/bin/bash
    param['title']="Test site"
    webDir="/var/www/test"
    projectName="new_test_site"
    projectDirName=${webDir}/${projectName}
    
    mkdir -p $projectDirName
    
    headvar=$(cat template.html | sed -e "s/~*~/${param[*]}/g")
    echo $headvar
    heres template.html:
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>~title~</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    as you can see, ~title~ is in the HTML tag, so I need bash to replace it with param['title'], if I do this:
    Code:
    cat template.html | sed -e "s/~*~/${param['title']}/g"
    it works (well kinda does, theres a bit of a glitch though), but I need the matched pattern to be the index of the array, but bash has a special meaning for array[*] so I don't know how. My plan is to have loads of these ~variables~ which bash will replace with values from the params array. Whats the best way of doing this? Would I be better off using awk?
    Last edited by frogwarrior; January 26th, 2014 at 09:52 AM.

  2. #2
    Join Date
    May 2005
    Location
    Lyon, France
    Beans
    916
    Distro
    Ubuntu Development Release

    Re: sed - How to use the matched result

    not sure i understand your logic but this might help you.
    Code:
    i=$(grep ~.*~ template.html | sed "s/.*~\(.*\)~.*/\1/")
    
    echo $i
    title
    
    echo ${param[title]}
    Test title
    
    sed "s/\(.*\)~\(.*\)~\(.*\)/\1${param[$i]}\3/g" template.html 
    <!DOCTYPE html>
    <html>
    <head>
    <title>Test title</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    James Dupin
    IT contractor
    Project delivery specialist
    http://fr.linkedin.com/in/jamesdupin

  3. #3
    Join Date
    Nov 2013
    Beans
    45

    Re: sed - How to use the matched result

    That didn't solve my problem because it seems to have loaded a string into i, not an array. I found another forum talking about this exact problem, and heres a working solution someone came up with:
    Code:
    echo
    echo "Using \${ template form and straight cvs form"
    echo
    OIFS=$IFS
    IFS=";"
    while read name val; do
        eval "tmpvars_$name=$val"
    done <params.txt;
    IFS=$OIFS
    eval echo "\"$(cat html.txt)\""
    unset ${!tmpvars_*}
    I haven't a clue how it works yet, but it gets the job done.

  4. #4
    Join Date
    May 2005
    Location
    Lyon, France
    Beans
    916
    Distro
    Ubuntu Development Release

    Re: sed - How to use the matched result

    an array index is a number, nothing else if not mistaken.

    you have to think about what you want to achieve with what means/tools before coding
    James Dupin
    IT contractor
    Project delivery specialist
    http://fr.linkedin.com/in/jamesdupin

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,248
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: sed - How to use the matched result

    this working solution is all kinds of fugly. A rule of thumb: eval=bad

    Code:
    #!/bin/bash
    
    declare -A param
    param=( [title]=TitLe
            [abc]=AbC
            [def]=o.O )
    
    sed -f <( for key in ${!param[@]}; do printf 's/~%s~/%s/g\n' "$key" "${param[$key]}"; done ) template.txt
    param is declared as associative array where strings are allowed as keys, by default bash arrays are integer-indexed.
    Colored part prepares a series of "s/~k~/param[k]/g" expressions that sed uses as an input file in 1 go. <( ... ) is a pseudo-file so sed which requires files with -f param happily gobbles it and runs.

    Code:
    $ cat template.txt 
    <!DOCTYPE html>
    <title>~title~</title>
    ~abc~ ~def~ ghi
    </html>
    $ ./html_template.sh 
    <!DOCTYPE html>
    <title>TitLe</title>
    AbC o.O ghi
    </html>
    Last edited by Vaphell; January 27th, 2014 at 03:21 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Bookmarks

Posting Permissions

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