terrid25
terrid25

Reputation: 1946

Find and replace strings using sed

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

Upvotes: 1

Views: 1699

Answers (3)

Bohemian
Bohemian

Reputation: 424953

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"

Upvotes: 3

Kent
Kent

Reputation: 195029

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

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

Upvotes: 2

BjoernD
BjoernD

Reputation: 4780

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

Upvotes: 1

Related Questions