Como reagrupar string com o mesmo comprimento usando awk

Como reagrupar string com o mesmo comprimento usando awk

Eu tenho uma longa lista de strings como esta:

_ah_
_asn_
_ai_
_errr_
_an_

E eu gostaria de transformá-lo assim

"_ah_ai_an_",
"_asn_",
"_errr_"

Eu tentei :

cat file | awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2-

O que essencialmente classifica por comprimento, mas o que estou tentando fazer é ter aqueles do mesmo comprimento na mesma linha com ","delimitadores As

Responder1

$ cat tst.awk
{
    lgth = length($0)
    sub(/_$/,"",strs[lgth])
    strs[lgth] = strs[lgth] $0
}
END {
    for (lgth in strs) {
        printf "%s\"%s\"", sep, strs[lgth]
        sep = "," ORS
    }
    print ""
}

$ awk -f tst.awk file
"_ah_ai_an_",
"_asn_",
"_errr_"

Responder2

Isso fornece a saída desejada:

awk -F_ '
    {strr[length] = strr[length]"_"$2}
    length > max {max=length}
    END {
        for (i=1;i<max;i++) {if (strr[i]) print "\"" strr[i]"_\","}
        print "\"" strr[max]"_\""
    }' file

Linhas com comprimento isão anexadas strr[i]com um _. No final, apenas percorremos strros elementos existentes e os geramos com "<element>_",, exceto o último elemento, que recebe no ,.

informação relacionada