AWK array from split not sorted -
i have (demo) text in variable arttext.
{1}: reporting problems , bugs. {2}: other freely available awk implementations. {3}: summary of installation. {4}: how disable gawk extensions. {5}: making additions gawk. {6}: accessing git repository. {7}: adding code main body of gawk. {8}: porting gawk new operating system. {9}: why derived files kept in git repository.
it 1 variable lines delimited indent.
indent = "\n\t\t\t";
i want loop through lines , check in each line.
so split array using indent
split(arttext,lin, indent);
then loop through array lin
l = 0; (l in lin) { print "l -- ", l, " lin[l] -- " ,lin[l] ; }
what lines of arttext beginning in line #4
l -- 4 lin[l] -- {3}: summary of installation. l -- 5 lin[l] -- {4}: how disable gawk extensions. l -- 6 lin[l] -- {5}: making additions gawk. l -- 7 lin[l] -- {6}: accessing git repository. l -- 8 lin[l] -- {7}: adding code main body of gawk. l -- 9 lin[l] -- {8}: porting gawk new operating system. l -- 10 lin[l] -- {9}: why derived files kept in git repository. l -- 1 lin[l] -- l -- 2 lin[l] -- {1}: reporting problems , bugs. l -- 3 lin[l] -- {2}: other freely available awk implementations.
(the original text has empty line @ beginning.)
the manual says split function:
the first piece stored in array[1], second piece in array[2], , forth.
how avoid problem?
why happening?
thanks.
in awk, arrays unordered. if happen come out in order, accidental.
in gnu awk, possible control order. example numerical ordering indices, use procinfo["sorted_in"]="@ind_num_asc"
:
$ awk -v arttext="$(cat file)" 'begin{procinfo["sorted_in"]="@ind_num_asc"; indent="\n\t\t\t"; split(arttext, lin, indent); (l in lin) print "l -- ", l, " lin[l] -- " ,lin[l] ;}' l -- 1 lin[l] -- {1}: reporting problems , bugs. l -- 2 lin[l] -- {2}: other freely available awk implementations. l -- 3 lin[l] -- {3}: summary of installation. l -- 4 lin[l] -- {4}: how disable gawk extensions. l -- 5 lin[l] -- {5}: making additions gawk. l -- 6 lin[l] -- {6}: accessing git repository. l -- 7 lin[l] -- {7}: adding code main body of gawk. l -- 8 lin[l] -- {8}: porting gawk new operating system. l -- 9 lin[l] -- {9}: why derived files kept in git repository.
alternatively, since array indices numerical, can loop numerically, using for (l=1;l<=length(lin);l++) print...
:
$ awk -v arttext="$(cat file)" 'begin{indent="\n\t\t\t"; split(arttext, lin, indent); (l=1;l<=length(lin);l++) print "l -- ", l, " lin[l] -- " ,lin[l] ;}' l -- 1 lin[l] -- {1}: reporting problems , bugs. l -- 2 lin[l] -- {2}: other freely available awk implementations. l -- 3 lin[l] -- {3}: summary of installation. l -- 4 lin[l] -- {4}: how disable gawk extensions. l -- 5 lin[l] -- {5}: making additions gawk. l -- 6 lin[l] -- {6}: accessing git repository. l -- 7 lin[l] -- {7}: adding code main body of gawk. l -- 8 lin[l] -- {8}: porting gawk new operating system. l -- 9 lin[l] -- {9}: why derived files kept in git repository.
multi-line versions
the gnu code shown on multiple lines looks like:
awk -v arttext="$(cat file)" ' begin{ procinfo["sorted_in"]="@ind_num_asc" indent="\n\t\t\t" split(arttext, lin, indent) (l in lin) print "l -- ", l, " lin[l] -- " ,lin[l] }'
and, alternate code is:
awk -v arttext="$(cat file)" ' begin{ indent="\n\t\t\t" split(arttext, lin, indent) (l=1;l<=length(lin);l++) print "l -- ", l, " lin[l] -- " ,lin[l] }'
Comments
Post a Comment