LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 11-21-2010, 06:07 PM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
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
 
Old 11-21-2010, 06:12 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Tack a g at the end of the expression
Code:
sed 's/u002C//g'


Cheers,
Tink

Last edited by Tinkster; 11-21-2010 at 06:13 PM. Reason: sample didn't match regex
 
1 members found this post helpful.
Old 11-21-2010, 08:08 PM   #3
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
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

Last edited by hattori.hanzo; 11-21-2010 at 08:32 PM.
 
Old 11-21-2010, 08:33 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
And what is the error it errors out with?
 
1 members found this post helpful.
Old 11-21-2010, 08:47 PM   #5
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
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

Last edited by hattori.hanzo; 11-21-2010 at 09:06 PM.
 
Old 11-22-2010, 05:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
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'
 
  


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
SED - remove last four characters from string 3saul Linux - Software 9 12-14-2015 05:43 PM
[SOLVED] Using sed to remove lines around a specified string twchambers Linux - General 1 06-04-2010 12:19 PM
[SOLVED] Using sed to remove text in fgrep string manwithaplan Linux - General 6 10-11-2009 02:00 AM
Trying to use sed to remove last line if it contains a certain string. slaxative Linux - Software 1 03-18-2008 03:13 AM
Remove string in sed twantrd Programming 7 09-13-2006 03:28 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration