Groups | Search | Server Info | Login | Register
Groups > comp.lang.awk > #9729
| From | jeojet@addr.invalid |
|---|---|
| Newsgroups | alt.comp.lang.awk, comp.lang.awk |
| Subject | Re: printing words without newlines? |
| Date | 2024-05-12 18:22 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <v1r1cd$2scb0$1@dont-email.me> (permalink) |
Cross-posted to 2 groups.
> <snip>
>I'm learning more AWK basics and wrote function to read file, sort,
>print. I use GNU AWK (gawk) and its sort but printing is harder to get
>working than anything... separate lines work, but when I use printf() or
>set ORS then use print (for words one line) all awk outputs (on FreeBSD
>UNIX 14 and Slackware GNU/Linux 15) is a space (and not even newline
>before shell prompt)... is this normal (and I made mistake?) or am I
>approaching it wrong? I recall BASIC prints new lines, but as I learned
>basic C and some derivatives, I'm used to newlines only being specified...
>------------------------------------------------------------------------
># print_file_words.awk
># pass filename to function
>BEGIN { print_file_words("data.txt"); }
>
># read two-column array from file and sort lines and print
>function print_file_words(file) {
># set record separator then use print
># ORS=" "
> while(getline<file) arr[$1]=$0
> PROCINFO["sorted_in"]="@ind_num_asc"
> for(i in arr)
> {
> split(arr[i],arr2)
> # output all words or on one line with ORS
> print arr2[2]
> # output all words on one line without needing ORS
> #printf("%s ",arr2[2])
> }
>}
> <snip>
I think you forgot that arr2 is now an array => you have to iterate over
it as well. There were also a few other coding errors, ie. not closing
the data.txt file; not declaring local vars in print_file_words:
--
$ cat test.awk
BEGIN { print_file_words("data.txt") }
function print_file_words(file, i,j) {
ORS = " "
PROCINFO["sorted_in"]="@ind_num_asc"
while (getline <file >0)
arr[$1] = $0
close (file)
for(i in arr) {
split(arr[i],arr2)
for (j in arr2)
print arr2[j]
}
ORS = "\n"
print ""
}
$ gawk -f test.awk
all are base belong to us your
--
Probably this is not the best way of doing things but I think you're
mainly just experimenting with sorting/printing so..
Back to comp.lang.awk | Previous | Next — Next in thread | Find similar
Re: printing words without newlines? jeojet@addr.invalid - 2024-05-12 18:22 +0000 Re: printing words without newlines? David Chmelik <dchmelik@gmail.com> - 2024-05-13 01:09 +0000 Re: printing words without newlines? David Chmelik <dchmelik@gmail.com> - 2024-05-13 02:13 +0000
csiph-web