Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I would like to be able to split an authorization code by the - character so that I can operate on each segment individually. I do not want to use external binaries (awk, grep) - this is to be as minimalistic as possible. What are some ways I can achieve this?

Here is an example auth code:

82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb

share|improve this question
    
Normally you would use cut. Why the limitation on external binaries though? –  Grant Aug 22 '12 at 17:35
    
This is on an embedded Linux device with limited access to them. –  Jay Hellwig Aug 22 '12 at 17:38

5 Answers 5

up vote 11 down vote accepted

Try using the Internal Field Separator (IFS):

AUTH_CODE='82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb'

OIFS=$IFS                   # store old IFS in buffer
IFS='-'                     # set IFS to '-'

for i in ${AUTH_CODE[@]}    # traverse through elements
do
  echo $i
done

IFS=$OIFS                   # reset IFS to default (whitespace)

Output:

82a8
bd7d
986d
9dc9
41f5
fc02
2c20
3175
097a
c1eb

By setting the Internal Field Separator, you split AUTH_CODE on the - character, allowing you to traverse through the newly-created elements in a foreach loop.

share|improve this answer
1  
thanks!!! that's exactly what I needed –  Jay Hellwig Aug 22 '12 at 17:49
    
If you haven't altered IFS from its default, you can ignore the saving the value of IFS into OIFS parts and just unset IFS when you're done. –  jrod Sep 12 '12 at 2:26

You can use IFS but this is simpler:

echo "82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb"- \
  | while read -d - i; do echo "$i"; done

Please take notice of added - after string to split — read will ignore the last token otherwise.

share|improve this answer
1  
this doesn't add the readability I want though, considering the operations on each element is rather intensive –  Jay Hellwig Aug 22 '12 at 17:50
2  
I don't understand. What's not readable? You don't need to do this in one line — you can have as many lines as you want between do and done. –  Tometzky Aug 22 '12 at 17:54

Another way with pattern matching and array:

AUTH_CODE='82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb'
array=( ${AUTH_CODE//-/ } )
for j in ${array[@]} ; do echo $j ; done
share|improve this answer
for i in {1..10}; do echo '82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb'|cut -d"-" -f$i;done
share|improve this answer
    
You might have missed the part about "no external binaries". –  nickgrim Sep 17 '12 at 8:48

Just use parameters expansion:

AUTH_CODE='82a8-bd7d-986d-9dc9-41f5-fc02-2c20-3175-097a-c1eb'
echo "${AUTH_CODE//-/$'\n'}"

Output:

82a8
bd7d
986d
9dc9
41f5
fc02
2c20
3175
097a
c1eb
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.