printing words without newlines?

Liste des Groupes 
Sujet : printing words without newlines?
De : dchmelik (at) *nospam* gmail.com (David Chmelik)
Groupes : alt.comp.lang.awk comp.lang.awk
Suivi-à : alt.comp.lang.awk comp.lang.awk
Date : 12. May 2024, 05:57:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v1pi7c$2b87j$1@dont-email.me>
User-Agent : Pan/0.158 (Avdiivka; 6a11104e)
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

Date Sujet#  Auteur
23 Dec 24 o 

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal