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
Post a Comment