linux - Split string using delimiter -


i trying split string using delimiter '|'. but, want '|' sample data in second example. how can achieve this?

f() {   local ifs='|'   local foo   set -f # disable glob expansion   foo=( $@ ) # deliberately unquoted   set +f   printf '%d\n' "${#foo[@]}"   printf '%s\n' "${foo[@]}" }  f 'un|dodecaedro|per|||tirare|per|i danni' 

expected output:

un dodecaedro per | tirare per danni  

there may way produce expected, here approach, hope using recent version of bash , here string supported

string='un|dodecaedro|per|||tirare|per|i danni'  awk '{     n=split($0,a,"|")     for(i=1;i<=n;i++)     {         if(length(a[i]) == 0 && length(a[i+1])==0)         {             print "|"; i+=1         }         else         {             print a[i]         }     }      }'  <<<"$string" 

resulting

 $ bash f  un  dodecaedro  per  |  tirare  per  danni 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -