Deutsch English Français Italiano |
<v1sid1$3bsil$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com> Newsgroups: alt.comp.lang.awk,comp.lang.awk Subject: Re: printing words without newlines? Date: Mon, 13 May 2024 10:18:40 +0200 Organization: A noiseless patient Spider Lines: 65 Message-ID: <v1sid1$3bsil$1@dont-email.me> References: <v1pi7c$2b87j$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 13 May 2024 10:18:41 +0200 (CEST) Injection-Info: dont-email.me; posting-host="3d5d09198ae2b77c093509ea5c7f0c0d"; logging-data="3535445"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XosOyZilXMaBS7mizf2pE" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:oPkvRSIyic/0jzM0KkFu0HscBqY= In-Reply-To: <v1pi7c$2b87j$1@dont-email.me> X-Enigmail-Draft-Status: N1110 Bytes: 3260 On 12.05.2024 06: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... IIUC you meanwhile have your script running, and probably code similar to BEGIN { print_file_words("data.txt"); } function print_file_words(file) { while (getline <file >0) arr[$1] = $0 PROCINFO["sorted_in"] = "@ind_num_asc" for (i in arr) { split (arr[i], arr2) printf "%s ", arr2[2] } printf "\n" } I suggest to add the '>0' test to your code, and also print a final "\n" so that your command line prompt doesn't overwrite your output. Note also that printf (like print) is a command, no function. Adding local variable declarations is also sensible to not get problems if you operate your code in other source code contexts. Janis > ------------------------------------------------------------------------ > # 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 >