LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed remove all occurrences in a string (https://www.linuxquestions.org/questions/linux-newbie-8/sed-remove-all-occurrences-in-a-string-845685/)

hattori.hanzo 11-21-2010 05:07 PM

sed remove all occurrences in a string
 
I have:

Code:

C:\u002Cdata\u002CDocuments
If I used:

Code:

sed 's/u0052//'
It only removes the first occurrence. How can I remove all occurrence in the line?

Thanks & Regards

Tinkster 11-21-2010 05:12 PM

Tack a g at the end of the expression
Code:

sed 's/u002C//g'


Cheers,
Tink

hattori.hanzo 11-21-2010 07:08 PM

Thanks.

Just a bit more on what I am doing. From the command line it works fine:

Code:

[me@host scripts]$ FOLDER=$(echo "C:\u002Cdata\u002CMy Files" | sed 's/C:\\/\//' | sed 's/u005C//g' | tr '\' '/' | sed 's/ /\\ /')
[me@host scripts]$ echo $FOLDER
/u002Cdata/u002CMy\ Files

But when I put this in a my script it errors out.

Code:

FPATH="C:\u002Cdata\u002CMy Files"
FOLDER=$(echo "$FPATH" | sed 's/C:\\/\//' | sed 's/u005C//g' | tr '\' '/' | sed 's/ /\\ /')

I think its something funny going on with the space in between My Files but not sure how to handle this.

Thanks & Regards

Tinkster 11-21-2010 07:33 PM

And what is the error it errors out with?

hattori.hanzo 11-21-2010 07:47 PM

It came with

Code:

sed: -e expression #1, char 11: unknown option to `s'
So I put in back ticks and now the command works fine

Code:

FPATH="C:\u002Cdata\u002CMy Files"
FOLDER=$(`echo "$FPATH" | sed 's/C:\\/\//' | sed 's/u002C//g' | tr '\' '/' | sed 's/ /\\ /'`)

But the result $FOLDER is not what I was expecting:

Code:

C:/data/My:
I am looking for this:

Code:

/data/My\ Files
UPDATE: Solved

Code:

FPATH="C:\u002Cdata\u002CMy Files"
FOLDER=$(echo "$FPATH" |  sed 's/u005C//g' | tr '\' '/' | sed 's/C://')
find /mnt/win"$FPATH"

Thanks Tinkster for the input.

Thanks & Regards

grail 11-22-2010 04:46 AM

May I also suggest that when dealing with the slash (/) that you use an alternate symbol for search and replace in sed to make it clearer.
eg
Code:

FOLDER=$(echo "$FPATH" |  sed 's@C:\\@/@')
Here is a sed alternative for the whole line:
Code:

FOLDER=$(echo "$FPATH" | sed -r -e 's@(C:|u002C)@@g' -e 's@\\@/@g'


All times are GMT -5. The time now is 12:17 PM.