Groups | Search | Server Info | Login | Register


Groups > alt.comp.lang.awk > #3

Re: printing words without newlines?

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Bruce Horrocks <07.013@scorecrow.com>
Newsgroups alt.comp.lang.awk, comp.lang.awk
Subject Re: printing words without newlines?
Date Sun, 12 May 2024 09:52:51 +0100
Lines 50
Message-ID <e0be0c38-e14e-45ba-ac87-5e2e4bd4f5cd@scorecrow.com> (permalink)
References <v1pi7c$2b87j$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net 7ZtMdNQRJovVo5ny6k2H8At5yB19aN7do6Eks3MBz/5ywIyQoX
Cancel-Lock sha1:BLcI4ks489ShB9fmBbQu/5jhqiY= sha256:tmG20+BsEf+1uT8Sitmd452NSzhShow/PidS5IbnH+E=
User-Agent Mozilla Thunderbird
Content-Language en-GB
In-Reply-To <v1pi7c$2b87j$1@dont-email.me>
Xref csiph.com alt.comp.lang.awk:3 comp.lang.awk:9726

Cross-posted to 2 groups.

Show key headers only | View raw


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.

-- 
Bruce Horrocks
Surrey, England

Back to alt.comp.lang.awk | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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