LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-07-2003, 09:17 AM   #1
pe2338
Member
 
Registered: Dec 2002
Location: Bucharest,RO
Distribution: debian etch, sarge and sid
Posts: 407

Rep: Reputation: 30
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
 
Old 10-07-2003, 09:45 AM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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
 
Old 10-07-2003, 09:52 AM   #3
art15t
LQ Newbie
 
Registered: Oct 2003
Posts: 9

Rep: Reputation: 0
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
 
Old 10-07-2003, 10:25 AM   #4
pe2338
Member
 
Registered: Dec 2002
Location: Bucharest,RO
Distribution: debian etch, sarge and sid
Posts: 407

Original Poster
Rep: Reputation: 30
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 
 
Old 10-07-2003, 10:28 AM   #5
pe2338
Member
 
Registered: Dec 2002
Location: Bucharest,RO
Distribution: debian etch, sarge and sid
Posts: 407

Original Poster
Rep: Reputation: 30
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 ...
 
Old 10-07-2003, 12:41 PM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tomsrtbt hangs with 'LIL' Daveb3 Linux - Newbie 1 05-27-2005 04:40 PM
URMPI... lil help admstng Mandriva 2 12-28-2004 09:58 PM
LIL problem alvincks Red Hat 5 10-10-2003 06:32 PM
need a lil c++ help Patchorus Programming 7 09-16-2003 01:05 AM
3 lil questions.... raistrick Linux - Newbie 6 08-09-2003 06:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration