Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

commands

SOLVED
Go to solution
Highlighted
Shivkumar
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
Rajeev Shukla
Honored Contributor
Solution

Re: commands

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

James R. Ferguson
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...
Shivkumar
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
Patrick Wallek
Honored Contributor

Re: commands

A solution using awk:

echo ":[rosePink]" | awk -F [ '{print $2}' | awk -F ] '{print $1}'
James R. Ferguson
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...
Hein van den Heuvel
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.
Hein van den Heuvel
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.
Shivkumar
Super Advisor

Re: commands

Thanks James!!

I respect your choice of tools as always.

Best Regards,
Shiv
Arturo Galbiati
Esteemed Contributor

Re: commands

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


HTH,
Art
Dennis Handly
Acclaimed Contributor

Re: commands

>Arturo: tr -d [:punct:]

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

Re: commands

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

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