Groups | Search | Server Info | Login | Register
Groups > comp.lang.awk > #9738
| Path | csiph.com!weretis.net!feeder8.news.weretis.net!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> (permalink) |
| 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= |
| Xref | csiph.com alt.comp.lang.awk:15 comp.lang.awk:9738 |
Cross-posted to 2 groups.
Show key headers only | View raw
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
Back to comp.lang.awk | Previous | Next — Previous in thread | Next in thread | Find similar
printing words without newlines? David Chmelik <dchmelik@gmail.com> - 2024-05-12 04:57 +0000
Re: printing words without newlines? Bruce Horrocks <07.013@scorecrow.com> - 2024-05-12 09:52 +0100
Re: printing words without newlines? Bruce Horrocks <07.013@scorecrow.com> - 2024-05-12 09:55 +0100
Re: printing words without newlines? gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-12 12:11 +0000
Re: printing words without newlines? David Chmelik <dchmelik@gmail.com> - 2024-05-13 02:04 +0000
Re: printing words without newlines? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-13 16:49 +0000
Re: printing words without newlines? gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-13 06:56 +0000
Re: printing words without newlines? gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-13 14:53 +0000
Resurrecting an old thread (Was: printing words without newlines?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-07-15 18:10 +0000
Re: printing words without newlines? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-13 10:18 +0200
Re: printing words without newlines? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-13 17:17 +0000
Re: printing words without newlines? gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-13 17:26 +0000
Re: printing words without newlines? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-05-13 23:33 +0000
Array indices are small integers? (Was: printing words without newlines?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-14 13:40 +0000
Re: printing words without newlines? Ed Morton <mortonspam@gmail.com> - 2024-05-16 08:11 -0500
Re: printing words without newlines? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-05-16 15:55 +0200
Once upon a time... (Was: printing words without newlines?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-16 14:15 +0000
Re: Once upon a time... (Was: printing words without newlines?) gazelle@shell.xmission.com (Kenny McCormack) - 2024-05-16 15:17 +0000
Re: printing words without newlines? Ed Morton <mortonspam@gmail.com> - 2024-05-16 19:40 -0500
csiph-web