BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

commands

SOLVED
Go to Solution
Super Advisor

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
11 REPLIES
Honored Contributor

Re: commands

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

Acclaimed Contributor

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...
Super Advisor

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
Honored Contributor

Re: commands

A solution using awk:

echo ":[rosePink]" | awk -F [ '{print $2}' | awk -F ] '{print $1}'
Acclaimed Contributor

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...
Honored Contributor

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.
Highlighted
Honored Contributor

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.
Super Advisor

Re: commands

Thanks James!!

I respect your choice of tools as always.

Best Regards,
Shiv
Esteemed Contributor

Re: commands

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


HTH,
Art
Acclaimed Contributor

Re: commands

>Arturo: tr -d [:punct:]

Yes, this will work, provided Shivkumar wants to remove ALL punctuation. White space is left.
Trusted Contributor

Re: commands

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

$ echo :[rosePink] | sed 's/:\[\(.*\)\]/\1/'
rosePink