Bash sed problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash sed problem
# 1  
Old 09-08-2010
Bash sed problem

Hi,

I need to escape slashes in my text, so I use this line:

Code:
search_string=`echo $var | sed 's@/@\\\/@g'`

I expect that to replace a slash with a backslash followed by a slash. That works nicely, but it has a problematic side-effect. If there are two spaces in the var it replaces them with one space. For example:

Code:
var="test1/test2<space><space>test3"
search_string=`echo $var | sed 's@/@\\\/@g'`
echo "$search_string"

produces test1\/test2<space>test3

What happened to the other space between test2 and test3?

(by <space> I mean one space character)
# 2  
Old 09-08-2010
Code:
search_string=`echo "$var" | sed 's@/@\\\/@g'`

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 09-08-2010
Thanks. What's the deal? It doesn't preserve spaces unless you surround it with quotes?
# 4  
Old 09-08-2010
Quote:
Originally Posted by RickS
Thanks. What's the deal? It doesn't preserve spaces unless you surround it with quotes?
that is correct - whitespace is removed by the shell.

I suggest removing it via the built-in functionality rather then use sed.

ie:
Code:
search_string=${var//\//\\/}

This User Gave Thanks to frank_rizzo For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Debian

Using sed with bash variables

Hi Guys I have another problem I'm trying to solve and hope that some one can help me here. This is the scenario: I have a file and I want to add a line on the 3rd line of the file using a bash script. but instead its adding the the bash variable $WEBSITE. Below is the bash script I'm... (6 Replies)
Discussion started by: linuxjunkie
6 Replies

2. Shell Programming and Scripting

Wget in bash using sed and awk

In the bash below when the program is opened the download function runs and downloads the getCSV file and on the screen "Downloading getCSV.csv:%" displays and when it completes the menu function is called. However, as of now the bash opens and closes after a few seconds and I'm not sure... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Dummies Questions & Answers

sed Or Grep Problem OR Terminal Problem?

I don't know if you guys get this problem sometimes at Terminal but I had been having this problem since yesterday :( Maybe I overdid the Terminal. Even the codes that used to work doesn't work anymore. Here is what 's happening: * I wanted to remove lines containing digits so I used this... (25 Replies)
Discussion started by: Nexeu
25 Replies

4. Shell Programming and Scripting

Bash and sed compatibility

Hi i am having a stange problem. Basically for my sed script used within bash i can unly use a single Quote with sed '. (if i use " then i get command garbled error with sed ) The problem with this is BASH does not interprit variables within single quote ' so we cannot use any of the BASH... (7 Replies)
Discussion started by: vash
7 Replies

5. Shell Programming and Scripting

[BASH] Problem with a sed -n statement

Hey all, So I've been banging my head against this for a few hours (:wall:) and I can't see whats wrong with it, each part seems to work fine on its own when entered at command line, but then it falls down when pulled together. I'm writing a script to translate fractional atomic coordinates... (3 Replies)
Discussion started by: EdinburghLad
3 Replies

6. Shell Programming and Scripting

Delete lines with SED in Bash?

Hello everyone I'm doing a program in bash and wanted to know how can I do to delete document lines the words not ending in S with SED, ie, show only those ending with the letter S. I probe with: sed -e /$.*/d "$file" | more to delete all lines NOT ending in S but not work!... (3 Replies)
Discussion started by: adiegorpc
3 Replies

7. Shell Programming and Scripting

using Sed in Solaris bash

I am trying to execute a script with sed that works well in ksh(Linux) however in bash(solaris 8) though it does not give any errors the output file becomes 0 byte. header of the script: ksh:2$ head news.ksh #!/bin/ksh... (2 Replies)
Discussion started by: acharania2011
2 Replies

8. UNIX for Dummies Questions & Answers

$ in sed under tcsh vs bash

In bash, I can match the ' character in a substition involving the line ending symbol $, easily. In tcsh I ran into a problem. Code: sed "s/$/'/g" filename sed "s/$/'/g" < filename sed -e "s/$/'/g" filename Unmatched '. Where can I find out why this is the case? (2 Replies)
Discussion started by: uiop44
2 Replies

9. Shell Programming and Scripting

BASH -- sed -- variable

Hi, I'm new at bash scripting -- can anyone here help me about the sed command? I need to be able to edit and or delete a text from an outside file ie file.txt -- I'm passing a variable and not a string I was thinking of something like echo -n "What do you want to edit?: " read edit sed... (1 Reply)
Discussion started by: Imajean
1 Replies

10. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question