1838661 Members
12597 Online
110128 Solutions
New Discussion

Variable manipulation

 
SOLVED
Go to solution
Sanjay Tailor
Frequent Advisor

Variable manipulation

Hello,

I am writing a shell script. I have a variable named INPUT which looks like 2.B3.2DCF
I need to create another variable with the third field ( 2DCF ) of the variable INPUT. Is there a way of doing this? I have tried "cut -d . -f3" but I cannot seem to pass INPUT to the cut. Is there another way?

Thanks for your help,
Sanjay.
6 REPLIES 6
Pedro Sousa
Honored Contributor

Re: Variable manipulation

Hi Sanjay!
The only thing you're missing is ".
try this:
echo $INPUT |cut -d "." -f3
good luck.
James R. Ferguson
Acclaimed Contributor

Re: Variable manipulation

Hi Sanjay:

You can also use 'awk':

# X=2.B3.2DCF
# echo $X|awk -F. '{print $3}'

...JRF...
Mladen Despic
Honored Contributor
Solution

Re: Variable manipulation

It should work without quotes too:

newvariable=`echo $INPUT | cut -d . -f3`
Pedro Sousa
Honored Contributor

Re: Variable manipulation

Hi again.
from the 'cut' man pages:

"The character following -d is the field delimiter (-f option only). Default is tab. Space or other characters with special meaning to the shell must be quoted. Adjacent field delimiters delimit null fields."
regards.
Pedro Sousa
Honored Contributor

Re: Variable manipulation

Oops.
It seems Mladen was correct.
Sanjay Tailor
Frequent Advisor

Re: Variable manipulation

Hello,

Thanks for your help. Even without the quotes the cut does work.

Thanks again,