2

input (in variable as $VAR) :

'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'dum' : x'dum' 'test' : x'test' 'poo' : x'poo' 'D2-dog' : x'D2-dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass'

I want output as :

yoo welcome we dum test poo D2-dog ant rat xmass

I do not want output as :

yoo
welcome
we
dum
test
poo
D2-dog
ant
rat
xmass

I tried echo $VAR | sed "s/' : x'//g" But it's messy and it's not what I am expecting.

Can we use cut or tr or sed ?

Edit after Alex answer: I have problem with different text :

echo "'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'test' : x'test' 'poo' : x'poo' 'dog' : x'dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass' 'exp' : x'exp' 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'" | sed -e "s/'\([^']*\)' : x'\1'/\1/g"

I get output as

yoo welcome we test poo dog ant rat xmass exp 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'

Strange and couldn't figure out the problem!

3

sed -e "s/'\([^']*\)' \?: x'\1'/\1/g"

run like this:

$ MYVAR="'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'dum' : x'dum' 'test' : x'test' 'poo' : x'poo' 'D2-dog' : x'D2-dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass'"
$ echo "$MYVAR" | sed -e "s/'\([^']*\)' \?: x'\1'/\1/g"
yoo welcome we dum test poo D2-dog ant rat xmass

This works, because \X back-references can also appear as part of the extended search pattern.

Edit: Changed RexExp to make the space before the colon optional

  • Thanks @Alex. But I have small problem when i try the same with following text, it wont work. echo "'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'test' : x'test' 'poo' : x'poo' 'dog' : x'dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass' 'exp' : x'exp' 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'" | sed -e "s/'\([^']*\)' : x'\1'/\1/g" I get out put as yoo welcome we test poo dog ant rat xmass exp 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'. Not sure why it has problem from 'S-123'. Can you help me with this ? – Kasino Aug 5 '16 at 16:13
  • @Kasino: sometimes your input has a space before the colon. I changed the regexp to make the space in front of the colon optional. Try the new regexp – Alex Stragies Aug 5 '16 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.