Path: csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Chmelik Newsgroups: alt.comp.lang.awk,comp.lang.awk Subject: Re: printing words without newlines? Date: Mon, 13 May 2024 02:13:28 -0000 (UTC) Organization: A noiseless patient Spider Lines: 60 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 13 May 2024 04:13:29 +0200 (CEST) Injection-Info: dont-email.me; posting-host="80c8003b504caa34e7ee7bc79149bb10"; logging-data="3390285"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+i0irFEBUkAkStgWqbfOlnYQmognc8+DY=" User-Agent: Pan/0.158 (Avdiivka; 6a11104e) Cancel-Lock: sha1:kTtS7ywIFFwKPxJSiBv1EQx9A9U= Xref: csiph.com alt.comp.lang.awk:9 comp.lang.awk:9732 On Sun, 12 May 2024 18:22:05 -0000 (UTC), jeojet wrote: >> >>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> 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]) >> } >>} >> > > 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 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 My original works after rebooting after discussion in main thread (without 'Re') but thanks for instruction to close file, though I don't know you need to pass in i--not used outside. It's odd iterating over arr2 even still prints all words (wrong order) because the way I used arr2 it only ever had one number and one word--its point was to split out & get word, then for the next i, it's split again onto arr2 which is erased/updated.