1

I am trying to replace strings in a document that enclosed in single quotes, to a string without.

'test' -> test

Is there a way using sed I can do this?

Thanks

3 Answers 3

3

This will remove single quotes from around any quoted word, and leave other quotes alone:

sed -E "s/'([a-zA-Z]*)'/\1/g" 

When tested:

foo 'bar' it's 'OK' --> foo bar it's OK

Notice that it left the quote in it's intact.

Explanation:

The search regex '([a-zA-Z]*)' is matching any word (letters, no spaces) surrounded by quotes and using brackets, it captures the word within. The replacement regex \1 refers to "group 1" - the first captured group (ie the bracketed group in the search pattern - the word)

FYI, here's the test:

echo "foo 'bar' it's 'OK'" | sed -E "s/'([a-zA-Z]*)'/\1/g"
Sign up to request clarification or add additional context in comments.

Comments

2

removing single quotes for certain word (text in your example):

kent$  echo "foo'text'000'bar'"|sed "s/'text'/text/g"
footext000'bar'

1 Comment

can be a bit DRYer: `sed "s/'(test)'/\1/g"
1

How about this?

$> cat foo
"test"
"bar"
baz "blub"

"one" "two" three

$> cat foo | sed -e 's/\"//g'
test
bar
baz blub

one two three

Update As you only want to replace "test", it would more likely be:

$> cat foo | sed -e 's/\"test\"/test/g'
test
"bar"
baz "blub"

"one" "two" three

3 Comments

I don't want to find every single place where the quotes are though. Just around one word, which is 'test'
Ah. That wasn't clear from your question. I'll edit my answer
@terrid25, really important to put these details in the question, not buried in comments. Please edit your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.