Deutsch English Français Italiano |
<20240513100418.652@kylheku.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: alt.comp.lang.awk,comp.lang.awk Subject: Re: printing words without newlines? Date: Mon, 13 May 2024 17:17:05 -0000 (UTC) Organization: A noiseless patient Spider Lines: 47 Message-ID: <20240513100418.652@kylheku.com> References: <v1pi7c$2b87j$1@dont-email.me> Injection-Date: Mon, 13 May 2024 19:17:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="d193b57106c428b2e9472f003e3e83e1"; logging-data="3777165"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jSCqYWuCPllglKaPmCEzXP2Sif2SbZCg=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:cj/6h7CZteijn1C/DkgFzM9xk6w= Bytes: 2096 On 2024-05-12, David Chmelik <dchmelik@gmail.com> wrote: > # sample data.txt > 2 your > 1 all > 3 base > 5 belong > 4 are > 7 us > 6 to $ awk '{ if ($1 > max) max = $1; rank[$1] = $2 } END { for (i = 1; i <= max; i++) if (i in rank) { printf("%s%s", sep, rank[i]); sep = " " } print "" }' data.txt all your base are belong to us We do not perform any sort, and so we don't require GNU extensions. Sorting is silly, because data is already sorted: we are given the positional rank of every word, which is a way of capturing order. All we have to do is visit the words in that order. We can do that by iterating an index i from 1 to the highest index we have seen. If there is a rank[i] entry, then we print it. (We do this "(i in rank)" check in case there are gaps in the rank sequence.) After we print one word, we start using the " " separator before all subsequent words. If we must sort, there is the sort utility: $ sort -n data.txt | awk '{ printf("%s%s", sep, $2); sep = " " }' && echo all your base are belong to us Also, if we can suffer a spurious trailing space: $ sort -n data.txt | awk '{ print $2 }' | tr '\n' ' ' && echo all your base are belong to us