LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-09-2006, 03:07 PM   #1
fatsheep
Member
 
Registered: Sep 2006
Posts: 79

Rep: Reputation: 15
Need help filtering out sed's "special" characters


I'm writing a simple package management bash script. Basically it just takes a package installed into /pkg/<PACKAGENAME> and then symlinks the files to the correct locations. It works fine, the only problem is that I have to put the path to the specific package inside the script (i.e. "/pkg/binutils-2.17"). So I thought instead of doing that, I'd use variables and have the user input the variables.

The problem I'm running into now is that I need to edit these variables. I'm using sed to do this and when I pass a variable equal to something like "/pkg/binutils-2.17/usr", sed thinks the "/" chars are delimters and that the "." chars means "any character". So I need to escape all of sed's "special" characters with a "\" like it says in the title. So I would need to change the above variable to "\/pkg\/binutils-2\.17\/usr"

Here's the setup I've got now for escaping these "special" characters:

Code:
### THESE ARE THE ORIGINAL VARIABLES WITHOUT ESCAPING

# $system = location of system's root directory.
system="/home/Desktop/sources"
# $pkgsdir = location of directory that stores the packages.
pkgsdir="$system/pkg"
# $pkg = directory of the package being installed.
pkg="$pkgsdir/lynx-2.8.6"

### THESE ARE THE NEW VARIABLES WITH ESCAPING

sed_system=`echo $system | sed 's@\/@\\\/@g' | sed 's@\.@\\\.@g' | sed 's@\\$@\\\$@g' | sed 's@\\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
sed_pkgsdir=`echo $pkgsdir | sed 's@\/@\\\\/@g' | sed 's@\\.@\\\\.@g | sed 's@\\$@\\\\$@g' | sed 's@\\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
sed_pkg=`echo $pkg | sed 's@\/@\\\\/@g' | sed 's@\\.@\\\\.@g' | sed 's@\\$@\\\\$@g | sed 's@\^@\\\\^@g' | sed 's@\*@\\\\*@g'`
It doesn't work very well (making changes to the sed syntax seems to break it), it's complex, and I need to do a lot of editing to make a simple change. What would be a better way to escape the special characters?

Any help is, as always, greatly appreciated.

Last edited by fatsheep; 11-09-2006 at 03:11 PM.
 
Old 11-09-2006, 03:42 PM   #2
fordeck
Member
 
Registered: Oct 2006
Location: Utah
Posts: 520

Rep: Reputation: 61
Could you post an example of the variable after your edit so I know what your start and end requirments are.
 
Old 11-09-2006, 04:54 PM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You are not enforced to use the /'s as delimiters
Code:
echo 'delamiters' | sed -e-'s#dela#deli#'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Print "special" characters blas Linux - Newbie 3 02-09-2005 03:40 PM
"Real time" Apache log filtering with Perl skelly Programming 1 07-01-2004 02:24 PM
"Real-time" Apache Log Filtering skelly Linux - Software 1 06-30-2004 02:16 PM
need help, on how to access quickly to special characters like "ñ" or "á"? Motaro Linux - Newbie 1 12-31-2003 11:53 AM
"Special" characters... Bjornkri Linux - Newbie 11 06-08-2003 07:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 PM.

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