How to extract a substring of $0, in awk, when that substring's position and length is based on the positions of fields in the original $0?It effectively equates to removing the first n.lead fields and the last n.trail fields and all those fields' leading and trailing delimiters, from the original $0    Here is an example: It is only known that the range is $4 to $8 inclusive.The delimiter is any number of spaces and/or a single comma, eg.   ,  or just ,... and the leading delimiter is to be ignored.            Input:    a  a  a   X marks   the   start,   Y   marks the  end  Expected Output: X marks   the   start,     Y
In awk, how to print a range of fields with the original $0 delimiters intact?
text processing;awk
Awk doesn't remember the field positions or the delimiter strings. You'll have to find out the field positions manually. It's not very hard.echo    a  b  c   X marks   the   start,   Y   marks the  end   |awk '{    i=1; n=1; tmp=$0;  # i=field number, n=column number    while (match(tmp, / *, *| +/)) {        A[i]=n; B[i]=n+RSTART-1;     # A[i],B[i] = start,end of delimiter i        ++i; n+=RSTART+RLENGTH-1;        tmp=substr(tmp,RSTART+RLENGTH)    }    print substr($0, A[5], B[9]-A[5])   # start at 4+1 because the first field is empty}'