unix - How to find sub-strings of a data string in korn-shell by their label -


i have data string contains multiple properties in following format: pno25|dno9|nwt153|nam579|npc3|

so properties pno, dno, nwt, nam, npc

note properties not in same order , values may have varying lengths (i.e. have dno9 above value of dno 9 come dno23 or dno152 values 23 or 152 respectively)

i need able retrieve values properties data string.

what best approach pulling value of property data string in korn shell script? simplicity sake lets retrieve value of dno.

i not experienced shell programmer... , @ wits end here having tried best googling korn-shell, using awk, grep etc...

just extract 1 tag value, can try this

$ awk -f'|' '{for(i=1;i<=nf;i++) if(match($i,/^dno/)) {sub(/dno/,"",$i); print $i}}' file  9 

to dictionary output of name:value pairs, perhaps easier...

$ tr '|' '\n' <file |    sed -r 's/([a-z]+)([0-9]+)/\1:\2/g;/^$/d' |    sort  dno:9 nam:579 npc:3 nwt:153 pno:25 

Comments