LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   want to escape forward slash, i tired but not working.. statement garbled (https://www.linuxquestions.org/questions/linux-newbie-8/want-to-escape-forward-slash-i-tired-but-not-working-statement-garbled-878340/)

vaibhavs17 05-02-2011 09:59 AM

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$/

David the H. 05-02-2011 10:16 AM

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.

vaibhavs17 05-02-2011 11:14 AM

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. (Post 4343959)
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.


catkin 05-02-2011 12:01 PM

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

Robhogg 05-02-2011 12:24 PM

Quote:

Originally Posted by vaibhavs17 (Post 4344036)
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?



All times are GMT -5. The time now is 01:05 AM.