LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-02-2009, 10:17 AM   #1
zokken
Member
 
Registered: Oct 2008
Posts: 34

Rep: Reputation: 15
Replacing Characters with sed


This shouldn't be hard, I just can't figure out how to do it. I'm feeding the output of a command into sed to remove these characters:

[
,
]
'

This is easy to do by piping the output through multiple sed commands, but this isn't efficient/elegant and I know it can be done using just one sed command. I'm just not having luck figuring out how to do it.

Any ideas?
 
Old 12-02-2009, 10:20 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

sed 's/[,'\[\]]//g' infile

The [ and ] are special and need to be escaped. The above command looks for either of the 4 chars given and replaces them with nothing.

Hope this helps.
 
Old 12-02-2009, 10:32 AM   #3
zokken
Member
 
Registered: Oct 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
Hi,

sed 's/[,'\[\]]//g' infile

The [ and ] are special and need to be escaped. The above command looks for either of the 4 chars given and replaces them with nothing.

Hope this helps.
This is having the same problem as something similar I tried earlier. This is what happens:

Code:
# grep accept_these_nonmembers list.cfg | sed 's/[,'\[\]]//g'
>
It leaves me hanging at the > prompt, as if it's expecting more input. I think the single quote in the search string is the problem. I tried escaping it, but that doesn't seem to work either:

Code:
# grep accept_these_nonmembers list.cfg | sed 's/[,\'\[\]]//g'
>
Any idea how to get around this?
 
Old 12-02-2009, 10:50 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

I gave the answer from the top of my head and forgot that ' is special.

This: sed "s/[,'\[\]]//g" infile would solve the ' character (use double quotes instead of single quotes around the replacement part). But a little test shows that \] doesn't seem to work and I have to look into that before giving an answer to that one.....
 
Old 12-02-2009, 10:53 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

sed "s/["]",'\[]//g" infile seems to work.

It does depend where the "]" is placed, it must be first it seems.

Hope this helps.
 
Old 12-02-2009, 11:04 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Put the sed command string in double-quotes.
Code:
sed "s/'/XX/g"   ##replaces ' with XX
Then, if a portion needs single quotes, enclose that portion in "'..'"

I read somewhere that you could "toggle" the quoting, but my tests just now do not support that. I had to unquote and then re-quote with the other type.

Code:
vari="XX"

sed "s/th/"'$vari'"/g" words   ##replaces th with XX

sed "s/th/'$vari'/g" words     ##replaces th with '$vari'
 
Old 12-02-2009, 12:19 PM   #7
zokken
Member
 
Registered: Oct 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Nice, thanks for the responses!
 
Old 12-02-2009, 01:14 PM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Two other things:

1) I recently experienced a sed variable substitution that would not work with either single or double quotes around it; I had to do it un-quoted.

2) to string multiple sed operations together without piping sed into sed into sed, just use a semicolon between each sed operation, like:
Code:
shell$ sed 's/this/that/g;s/something/else/g' "file"
Sasha
 
Old 12-02-2009, 01:31 PM   #9
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
There's also the shorter way:

Code:
tr -d "[,]'" < input > output
 
1 members found this post helpful.
Old 12-02-2009, 07:34 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,696
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
use the shell
Code:
$ string="my[str]in'g,"
$ echo ${string//[\[\],\']/}
mystring
 
0 members found this post helpful.
  


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
replacing with sed dipuasks Linux - General 13 12-31-2008 11:24 AM
Regarding replacing characters in log file rajaniyer123 Linux - General 4 04-12-2008 10:32 AM
bash replacing characters within a variable baks Programming 5 03-19-2007 12:33 AM
Replacing characters in file names? yuccabrevifolia Linux - Software 1 04-19-2006 01:31 PM
replacing characters in a file with others pcdude Linux - General 6 01-19-2005 12:29 AM


All times are GMT -5. The time now is 03:21 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration