Author Topic: [SOLVED] Bash .... Deleting Numbers from a String  (Read 360 times)

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
[SOLVED] Bash .... Deleting Numbers from a String
« on: July 29, 2010, 06:38:33 AM »
I thought to resurrect the old thread but decided a new one might be best.

This time it appears to be a very simple problem. An example is the easiest way to explain .......

I had to rewrite this ....  apologies .......

I have a variable which consists of a string of letters and allowed others like */-| and so on, followed by numbers. All the one string.

So it might be something like     aj&;?/jg12
The only part of the string that can be numerical are the last digit or two.
The length of the string is not fixed.
The only certainty is that there will be a number or two numbers at the end of the string.

I wish to discard the numbers .....  whether it is one or two.

I hope that explains things better.

Thanks for the support with my familiarising myself with some Bash commands.

regards.
« Last Edit: July 29, 2010, 07:53:11 AM by JohnBoy »

Offline tschommer

  • PCLinuxOS Tester
  • Hero Member
  • *******
  • Posts: 1890
  • MLU and BLU (Bacon lovin' user)
Re: Bash .... Text filtering again
« Reply #1 on: July 29, 2010, 07:20:06 AM »
JohnBoy,

I've tried this and it seems to work fine:

Code: [Select]
# myvar="aj&;?/jg12"
# mynewvar=`echo $myvar | sed 's/[0-9]*//g'`
# echo $mynewvar
aj&;?/jg

Is that what you were looking for?

Torsten
Our defense is in the preservation of the spirit which prizes liberty as the heritage of all men, in all lands, everywhere."
Abraham Lincoln --September 11, 1858 Speech at Edwardsville, Illinois

Offline Was_Just19

  • Hero Member
  • *****
  • Posts: 6852
  • MLU
Re: Bash .... Text filtering again
« Reply #2 on: July 29, 2010, 07:50:39 AM »
JohnBoy,

I've tried this and it seems to work fine:

Code: [Select]
# myvar="aj&;?/jg12"
# mynewvar=`echo $myvar | sed 's/[0-9]*//g'`
# echo $mynewvar
aj&;?/jg

Is that what you were looking for?

Torsten

Thanks again.
Yes that is what I meant apparently ....  haven't actually tried it as just now saw it, but it appears so.

I was coming back to post something I found ....  eventually ..... after I used better search terms ....  and this was it -- very similar to yours ......

Code: [Select]
myvar="aj&;?/jg12"
echo $myvar | sed 's/[0-9]*//g'
aj&;?/jg

Code: [Select]
myvar="aj&;?/jg12"
echo ${myvar//[0-9]/}
aj&;?/jg


All is good.

Thank you  :)
« Last Edit: July 29, 2010, 07:54:44 AM by JohnBoy »