LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2011, 09:59 AM   #1
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Rep: Reputation: 1
want to escape forward slash, i tired but not working.. statement garbled


Hi Team,
could you please help on below? I am trying to use sed command to repalce one string with other but somehow replacement string contains forwards slash hence getting the error statement garbled!
please help

from_row contains /wu9m<h-8$

Fix is require at
sql_text=`cat $staging_population_program | head -$before_lines | sed "s/\&\&1/$from_row/" | sed "s/\&\&2/$to_row/" | sed "s/\&\&3/$region/"`

nygespappd29:/data/apps/pnbos/scripts > ./populate_staging_posn.ksh -C /data/apps/pnbos/config/HK_EOD.cfg -f C
ALCSTG

START ./populate_staging_posn.ksh at Monday, May 2, 2011 03:07:30 PM BST
--> Running Staging POSN for ASIA at Monday, May 2, 2011 03:07:30 PM BST
--> Loading profit centres at Monday, May 2, 2011 03:07:30 PM BST
--> Splitting for multiple engines at Monday, May 2, 2011 03:07:48 PM BST
--> Running multiple engines at Monday, May 2, 2011 03:07:51 PM BST
sed: command garbled: s/\&\&2//wu9m<h-8$/
sed: command garbled: s/\&\&2//wu9m<h-8$/
sed: command garbled: s/\&\&1//wuCm<h-8$/
sed: command garbled: s/\&\&1//wuCm<h-8$/
 
Old 05-02-2011, 10:16 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

As for your problem, sed can use any basic ascii character as a separator, not just "s/x/y/". Try using "s|x|y|" instead, for example.

Also,
$(..) is recommended over `..`

PS: sed also has a -e option for applying multiple expressions at once. No need to pipe it through three instances.

Last edited by David the H.; 05-02-2011 at 10:19 AM. Reason: added PS
 
Old 05-02-2011, 11:14 AM   #3
vaibhavs17
Member
 
Registered: Mar 2009
Posts: 51

Original Poster
Rep: Reputation: 1
It is still not working, I tried what you have suggested:

Code:
 
#!/bin/ksh

from_row='/wu9m<h-8$'
to_row='Hi'
echo $from_row
sed "s|to_row|$from_row"
echo $to_row


nygespappd29:/data/apps/pnbos/scripts > ./hello.sh
/wu9m<h-8$
sed: command garbled: s|to_row|/wu9m<h-8$
Hi
I tired this as well, it hung:
Code:
#!/bin/ksh

from_row='/wu9m<h-8$'
to_row='Hi'
#echo $from_row
sed "s|to_row|$from_row|"
echo $to_row
echo "vaibhav"
exit 0
nygespappd29:/data/apps/pnbos/scripts > ./hello.sh
Quote:
Originally Posted by David the H. View Post
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

As for your problem, sed can use any basic ascii character as a separator, not just "s/x/y/". Try using "s|x|y|" instead, for example.

Also,
$(..) is recommended over `..`

PS: sed also has a -e option for applying multiple expressions at once. No need to pipe it through three instances.

Last edited by vaibhavs17; 05-02-2011 at 11:18 AM.
 
Old 05-02-2011, 12:01 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
In the first sed "s|to_row|$from_row" is missing the closing | so it errors.

In the second it is not so it doesn't error and starts reading stdin -- which does not come. Try sed "s|to_row|$from_row|" some_file_name
 
Old 05-02-2011, 12:24 PM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
Originally Posted by vaibhavs17 View Post
It is still not working, I tried what you have suggested:

Code:
 
...
echo $from_row
sed "s|to_row|$from_row" # there is no closing pipe character on the command here
echo $to_row
...
I tired this as well, it hung:
Code:
#!/bin/ksh
...
sed "s|to_row|$from_row|" # it's hanging because sed is waiting for something to process
                          # The original code you gave, piped the output from a cat command
                          # through sed. Also, this will replace the text-literal "to_row",
                          # not the contents of the variable $to_row
...
By default, if you enter a sed command, and don't pipe/redirect anything to it, it will wait for you to type something on the command line.
Code:
rob@rob-debian:~$ sed "s|$to_row|$from_row|"
Cheese
Cheese
Pickles
Pickles
Hello, how are you?
Hello, how are you?
Hi, how are you?
/wu9m<h-8$, how are you?
The sort of thing you need is:
Code:
#!/bin/bash
to_process="Hi, how are you?"
from_row='/wu9m<h-8$'
to_row='Hi'
processed=$(echo $to_process | sed "s|$to_row|$from_row|")
echo $processed 
exit 0
rob@rob-debian:~$ ./hello.sh
/wu9m<h-8$, how are you?
 
  


Reply

Tags
command line, escape, sed



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
Small problem with bash and forward slash Karsh Linux - Newbie 5 08-13-2010 07:55 PM
escape '*' in case statement bash junust Programming 2 01-23-2010 03:54 AM
Forward slash (/) in filenames. Oxagast Linux - Software 10 07-21-2009 09:04 PM
question mark and forward slash osc~ Slackware 2 08-16-2007 12:32 PM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:43 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