Re: commands (307 Views)
Reply
Super Advisor
Shivkumar
Posts: 1,207
Registered: ‎05-22-2005
Message 1 of 12 (307 Views)
Accepted Solution

commands

Hi,


From the below word

:[rosePink]

i want to remove : [, and ]

i just want to extract rosePink.

How do i do that ?

Thanks,
Shiv
Please use plain text.
Honored Contributor
Rajeev  Shukla
Posts: 1,402
Registered: ‎01-25-1999
Message 2 of 12 (307 Views)

Re: commands

If you have that word in a file say "aa" then do this
sed -e 's/:\[//' -e 's/\]//' aa

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 12 (307 Views)

Re: commands

Hi Shiv:

Another way:

# echo "I am :[rosePink] I am" | perl -ne 'print $1 if m%.*\[(.+)\].*%'

# perl -ne 'print $1 if m%.*\[(.+)\].*%' file

Regards!

...JRF...
Please use plain text.
Super Advisor
Shivkumar
Posts: 1,207
Registered: ‎05-22-2005
Message 4 of 12 (307 Views)

Re: commands

we don't want to use the word "i am and also don't want to use perl"..

is there any other alternative using greg/egrep or awk ?

best regards,
shiv
Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,711
Registered: ‎06-21-2000
Message 5 of 12 (307 Views)

Re: commands

A solution using awk:

echo ":[rosePink]" | awk -F [ '{print $2}' | awk -F ] '{print $1}'
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 12 (307 Views)

Re: commands

Hi (again) Shiv:

You wrote, "we don't want to use the word "i am and also don't want to use perl".

First, the example I showed simply extracts the string contained in square brackets --- which was exactly the extent of what you asked.

Second, you never indicated what tools you would accept and what tools you would not. Perl was my choice.

# echo ":[rosePink]"|perl -nle 'print $1 if m%\[(.+)\]%'

Regards!

...JRF...
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,579
Registered: ‎05-19-2003
Message 7 of 12 (307 Views)

Re: commands

$ echo :[rosePink] | awk -F [\]\[] '{ print $2}'
rosePink

Actually, as much as I often think Shiv shoudl use study the man pages a bit more, and just try some stuff to learn, this time he is onto something tricky.

The square brackets are tricky to specify as they are a critical part of regular expression.

As we all know, awk can use a regulare expression as field seperator. And boxed characters are a choicelist of sort. Pick "i" and "o" as seperators and this works...

$ echo :[rosePink] | awk -F [io] '{ print $2}'
seP

Now pick "[" and "]"... no go...
$ echo :[rosePink] | awk -F [[]] '{ print $2}'

Ok, so escape them.... no go...

$ echo :[rosePink] | awk -F [\[\]] '{ print $2}'

Now escape them, and re-order and it works:

$ echo :[rosePink] | awk -F [\]\[] '{ print $2}'
rosePink

At first I though i was battling the shell on top of awk, but even using a tiny awk program you need to take thenm out of order.

$ cat test.awk
BEGIN { FS="[][]" }
{ print "1=" $1 " 2=" $2 " 3=" $3 }
$ echo :[test]aaa | awk -f x.awk
1=: 2=test 3=aaa

grins,
Hein.
Please use plain text.
Honored Contributor
Hein van den Heuvel
Posts: 6,579
Registered: ‎05-19-2003
Message 8 of 12 (307 Views)

Re: commands

Hmm,

Shiv wrote> i want to remove : [, and ]

taking that original request literally the solution would be:

$ echo :[rosePink] | tr -d :[]
rosePink


Or with symbols:

$ x=:[rosePink]
$ y=$(echo $x | tr -d :[])
$ echo $x " - " $y
:[rosePink] - rosePink


Hein.
Please use plain text.
Super Advisor
Shivkumar
Posts: 1,207
Registered: ‎05-22-2005
Message 9 of 12 (307 Views)

Re: commands

Thanks James!!

I respect your choice of tools as always.

Best Regards,
Shiv
Please use plain text.
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 10 of 12 (307 Views)

Re: commands

Hi Shiv
this run fine on my Hp-UX11i:
% echo ":[rosePink]"|tr -d [:punct:]
rosePink


HTH,
Art
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,697
Registered: ‎03-06-2006
Message 11 of 12 (307 Views)

Re: commands

>Arturo: tr -d [:punct:]

Yes, this will work, provided Shivkumar wants to remove ALL punctuation. White space is left.
Please use plain text.
Trusted Contributor
Eric Raeburn
Posts: 137
Registered: ‎11-26-2002
Message 12 of 12 (307 Views)

Re: commands

I believe this will provide a concise solution, without forceably removing all punctuation:

$ echo :[rosePink] | sed 's/:\[\(.*\)\]/\1/'
rosePink
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation