Re: printing words without newlines?

Liste des GroupesRevenir à cl awk 
Sujet : Re: printing words without newlines?
De : 07.013 (at) *nospam* scorecrow.com (Bruce Horrocks)
Groupes : alt.comp.lang.awk comp.lang.awk
Date : 12. May 2024, 09:55:52
Autres entêtes
Message-ID : <8f790f04-5f16-4f07-8e0b-261b628baa7a@scorecrow.com>
References : 1 2
User-Agent : Mozilla Thunderbird
On 12/05/2024 09:52, Bruce Horrocks wrote:
On 12/05/2024 05:57, David Chmelik 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<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])
   }
}
------------------------------------------------------------------------
# sample data.txt
2 your
1 all
3 base
5 belong
4 are
7 us
6 to
 You need to set ORS in the BEGIN { } section (or on the command line).
 See <https://www.gnu.org/software/gawk/manual/html_node/Output-Separators.html> for an example - just replace the "\n\n" in the example with " " to see the effect you are looking for.
 
Let me re-phrase that: it would be better to set ORS in the BEGIN {} section. I'm not sure why yours is not working but with some commented out code and some not, your example is unclear.
If what I have suggested doesn't work for you then please re-post your exact code.
--
Bruce Horrocks
Surrey, England

Date Sujet#  Auteur
12 May 24 * printing words without newlines?19David Chmelik
12 May 24 +* Re: printing words without newlines?5Bruce Horrocks
12 May 24 i+- Re: printing words without newlines?1Bruce Horrocks
12 May 24 i`* Re: printing words without newlines?3Kenny McCormack
13 May 24 i +- Re: printing words without newlines?1David Chmelik
13 May 24 i `- Re: printing words without newlines?1Kaz Kylheku
13 May 24 +* Re: printing words without newlines?3Kenny McCormack
13 May 24 i`* Re: printing words without newlines?2Kenny McCormack
15 Jul 24 i `- Resurrecting an old thread (Was: printing words without newlines?)1Kenny McCormack
13 May 24 +- Re: printing words without newlines?1Janis Papanagnou
13 May 24 +* Re: printing words without newlines?4Kaz Kylheku
13 May 24 i`* Re: printing words without newlines?3Kenny McCormack
14 May 24 i `* Re: printing words without newlines?2Kaz Kylheku
14 May 24 i  `- Array indices are small integers? (Was: printing words without newlines?)1Kenny McCormack
16 May 24 `* Re: printing words without newlines?5Ed Morton
16 May 24  `* Re: printing words without newlines?4Janis Papanagnou
16 May 24   +* Once upon a time... (Was: printing words without newlines?)2Kenny McCormack
16 May 24   i`- Re: Once upon a time... (Was: printing words without newlines?)1Kenny McCormack
17 May 24   `- Re: printing words without newlines?1Ed Morton

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal