With gawk:
awk '{ for ( i=1;i<=length($0);i++) { if ( match(substr($0,i,1),/[[:alnum:]]/)) { cnt++;if ( cnt==5) { print substr($0,1,i) } } } }' <<< "1-a-bc-dxyz"
Read each character one at a time, and then if there is a pattern match for an alphanumeric character (using the match function), increase the cnt variable. When cnt gets the value 5, print the line we have seen so far (using the substr function)
Conclusion:
1-a-bc-d
source
share