LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   help with lil' script (https://www.linuxquestions.org/questions/linux-software-2/help-with-lil-script-101214/)

pe2338 10-07-2003 09:17 AM

help with lil' script
 
I know this is off topic, but I saw that the programming forum is kind of dead...

So my problem is: I want to repalce from a script the leading path /mnt/cdrom or something else given as an argument and to transform it into some other string...



the problem is with the /-es in the path...

they screw up the sed params....

I tried replacing static text weih the contents of a var and it works ...
the probl is with both vars....

Here is the script...
and is a GNU version 2.... if anybody wants it.

Code:

...... will be in the next post

ToniT 10-07-2003 09:45 AM

sed 's#a/source/path/#other/target/path#' < input.txt > output.txt
Or alternatively:
sed 's/a\/source\/path\//other\/target\/path/' < input.txt > output.txt

art15t 10-07-2003 09:52 AM

Yep thats it ToniT! Pe2338 needs to stop the shell from interpretting the "-" as an option to sed. This is most commonly done by preceding the meta-character with a \backslash-

ART

pe2338 10-07-2003 10:25 AM

Here is the script ....

tried various ways... obvoious from the comments...

Code:


#!/bin/bash

LL=17                        #Last line of comments
LC=$(( $LL - 6))        #Length - no of lines of introductory comments
#echo LC este $LC

#This script is inteneded to make a tool for the users that have many CDs,
#that contain scattered info and files and ususally forget where are located.
#
#        Synopsis : cdsearch <--scan <cdname>|--rmcd|--rmpath|--rmfile> [--comment <comment>]
#
#  Options:
#        --scan                : adds a new dir to the colection
#        --rmcd                : removes a cd from the colection (not implemented)
#        --rmpath        : removes a dir from the colection (not implemented)
#        --rmfile        : removes a file from the colection (not implemented)
#
#
#this is the last line of intoructory comments and should NOT be included (LL+1=this line)

if test $# -le 1
then
    cat $0 | head -n $LL | tail -n $LC | sed s/^\#//
elif test $# -ge 2
then
   
    while (($#>1));
    do
    case "$1" in
            "--scan")
                OPER=scan
                CDNAME=$2
                shift
                ;;
            "--rmcd"|"--rmpath"|"--rmfile")
                OPER="not implemented" ;;
            "--comment")
                COMMENT=$2
                shift ;;
        esac
        shift;
#        echo $*                #Let's see all the params now
#        echo i is $i
    done
   
    # Now we know what to do...
   
    case "$OPER" in
        "not implemented")
            echo operation not implemented
            exit 1
            ;;
        "scan")
            echo "Will try to scan and add a new CD/dir to the collection"
           
            #Let's find out the name of the cd database file and some other info
            if test -e "~/cdtool.conf";
            then
                CDDRIVE=`cat ~/cdtool.conf | grep CDDRIVE | sed s/^\ *CDDRIVE\ *\=\ *//`
                COLLECTION=`cat ~/cdtool.conf | grep DATABASE | sed s/^\ *DATABASE\ *\=\ *//`;
            else
                CDDRIVE=/mnt/cdrom
                COLLECTION=~/.cdtool/cddbase

                echo
                echo Drive : $CDDRIVE    Collection : $COLLECTION  CDName : $CDNAME
                echo
               

                if (test ! -d ~/.cdtool) ;
                then
                    mkdir ~/.cdtool;
                fi
                touch "$COLLECTION" ;
            fi
           
            echo -n "Starting $CDDRIVE path scan..."
           
            echo           
   
            #TMP=`mktemp`
            #rm -f "$TMP"
            #TMP=`echo $TMP | cut -d'/' -f3-`
            #echo "$TMP"
            #read
           
#            echo "\"$CDDRIVE\"" "\"$CDNAME\""

#            echo "$CDDRIVE" | sed s/"\/"/'XXXX'/

            CD=`echo "\\\"\"$CDDRIVE\\\"\"" ` #| sed s/"\/"/"\\\/"/ | sed s/"^"/"\""/`
#            echo $CD
           
#            echo "/mnt/cdrom" | sed --file="./add"
           

#            echo $CDDRIVE | sed s/$CD/"\\\"$CDNAME\\\" - "/
            echo $CDDRIVE | sed s/"\/mnt\/cdrom"/"\\\"$CDNAME - \\\""/
#            CN=`echo $CDNAME | tr "/" "\\\/" | tr ' ' "\\\ "`
#            echo $CDNAME | tr "/" "\\\/" | tr ' ' "\\\ "
#            CMD="sed s/\(\"$CD\"\)/\(\"$CN\"\)/"
           
#            echo $CMD $CD $CN
            read           
#            find $CDDRIVE -true -noleaf | cut -d'/' -f$FF- | sed s/\("$CD"\)/\("$CN"\)/  # | cat >>$COLLECTION
#            echo "done."
           
            ;;
    esac
   
fi


pe2338 10-07-2003 10:28 AM

Quote:

Originally posted by ToniT
sed 's#a/source/path/#other/target/path#' < input.txt > output.txt
Or alternatively:
sed 's/a\/source\/path\//other\/target\/path/' < input.txt > output.txt

The quotes stop the text within them to be interpreted, but I need that...

As you can see the heading is in a var $CDDRIVE and the replace is in $CDNAME....

Thanks fo the help ...

ToniT 10-07-2003 12:41 PM

Ok,

I'm sorry, but there are so many tries that I don't follow what are you exactly trying to do and what is the problem with it.
  • Want to have a sed script that doesn't choke to slashes?

    Use an other separator, like the # as used in my previous example or as
    Code:

    echo foo/bar/baz | sed s#foo/#ubl/e#
  • Want to have variables that are pre-substituted to the sed expression by shell?

    Don't use single quotes or leave variables out of quotes.
    Code:

    export JEE='joo/jaa' ;  echo foo/bar/baz | sed "s#foo/#ubl/|$JEE|e#"
    or
    export JEE='joo/jaa' ;  echo foo/bar/baz | sed 's#foo/#ubl/|'"$JEE"'|e#'

  • Want something else, what?


All times are GMT -5. The time now is 12:21 PM.