Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a parttern in a Variable

Status
Not open for further replies.

alhassani54

Programmer
Aug 1, 2002
27
0
0
GB
I have a variable as rec. rec will contain one or many of UNB possibly

rec=UNB123456789UNB2234567890UNB32345UNB4234567UNB52
I would like to change it to
rec1=UNB123456789
rec2=UNB2234567890
rec3=UNB32345
rec4=UNB4234567
rec5=UNB52

Tried to use regular expression but I could not get it right.
rec1=${rec%UNB*}
print $rec1
UNB123456789UNB2234567890UNB32345UNB4234567

I would like to use unix regular expresion.

Thanks
 
Hi,

Try this work around :
Code:
echo $rec |awk -F"UNB" '{split($0,t);for (i=2;i <= NF;i++) print "rec"i-1"=UNB"t[i]}'

You redirect the outpout to a file if you want to keep trace and then source it in your script.
 
How about:

Code:
rec=UNB123456789UNB2234567890UNB32345UNB4234567UNB52
set -- `echo $rec | sed 's/UNB/ UNB/g'`
rec1=$1
rec2=$2
rec3=$3
rec4=$4
rec5=$5


Annihilannic.
 
I like Annihilanic's sed statement. If you don't know how many bits you're going to get you could use an array

Code:
#!/bin/ksh

set -A arr $(echo $rec | sed 's/UNB/ UNB/g')
kount=0
while [[ $kount -lt ${#arr[@]} ]]
do
  echo "rec$(expr $kount + 1)  = ${arr[$kount]}"
  (( kount += 1 ))
done


Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top