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
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.
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.
<snip>
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])
}
}
<snip>
<snip>
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])
}
}
<snip>
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:
Of course, the whole point of this thread is that none of us has any
idea what OP is talking about or what his actual problem is. We can
only guess...
On 12/05/2024 09:52, Bruce Horrocks wrote:
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.
<snip>
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])
}
}
<snip>
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 <file >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<snip>
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.
# 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
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
I guess this is what you actually want:
{ A[$1] = $2 }
END {
len = length(A)
for (i=1; i<=len; i++)
printf("%s%s",A[i],i<len ? " " : "\n")
}
In article <e0be0c38-e14e-45ba-ac87-5e2e4bd4f5cd@scorecrow.com>,
Bruce Horrocks <07.013@scorecrow.com> wrote:
...
You need to set ORS in the BEGIN { } section (or on the command line).
This is demonstrably false. You can set ORS whenever/wherever you want. Whatever value it has when a plain "print" statement is executed, is what will be used. You are probably about thinking about the various variables that affect input parsing. These variables clearly must be set prior to the reading of the input, which usually means they need to be set in BEGIN (or via something like -F or -v on the command line).
One of my favorite idioms (and one that might actually be useful to OP) is:
# Print every 3 input lines as a single output line
# Yes, this single line is the whole program!
ORS = NR % 3 ? " " : "\n"
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.
Of course, the whole point of this thread is that none of us has any idea what OP is talking about or what his actual problem is. We can only guess...
(flow "data.txt"file-get-lines
(flow "data.txt"prinl
(defmacro dflow (. args)^(flow ,*(interpose 'prinl args)))
(macroexpand-1 '(dflow a b c d))(flow a prinl
(dflow "data.txt"file-get-lines
# sample data.txt
2 your
1 all
3 base
5 belong
4 are
7 us
6 to
$ awk '{
if ($1 > max) max = $1;
rank[$1] = $2
}
END {
for (i = 1; i <= max; i++)
if (i in rank) {
printf("%s%s", sep, rank[i]);
sep = " "
}
print ""
}' data.txt
all your base are belong to us
We do not perform any sort, and so we don't require GNU extensions. Sorting is
In article <20240513100418.652@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
...
(This version more complicated than it needs to be, but essentially the
same as what I posted earlier)
$ awk '{
if ($1 > max) max = $1;
rank[$1] = $2
}
END {
for (i = 1; i <= max; i++)
if (i in rank) {
printf("%s%s", sep, rank[i]);
sep = " "
}
print ""
}' data.txt
all your base are belong to us
We do not perform any sort, and so we don't require GNU extensions. Sorting is
But GNU extensions are good - especially since OP specifically mentioned using GAWK. And much more on-topic than Lisp (et al).
Final note: In fact, it has been established (on this newsgroup as well as empirically by me and others) that if the indices are small integers, you
get sorting for free (in GAWK, which, as noted, is all we care about). So, you don't even really need to mess with PROCINFO[]...
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)...
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
On 5/11/2024 11:57 PM, 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)...
[...]
------------------------------------------------------------------------
# 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=" "
Move the above to a BEGIN section so it is executed once total instead
of once per input line.
--- Synchronet 3.20a-Linux NewsLink 1.114[...]
A function definition called once from the BEGIN section isn't
called "once per input line".
In article <v2538p$1jmvm$1@dont-email.me>,
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
...
A function definition called once from the BEGIN section isn't
called "once per input line".
Especially since it is commented out, so it executes exactly zero times.
Actually setting ORS (or any other similar variable) inside a function >definition is not such a bad idea, in terms of modularity.
On 16.05.2024 15:11, Ed Morton wrote:
On 5/11/2024 11:57 PM, 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)...
[...]
------------------------------------------------------------------------ >>> # 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=" "
Move the above to a BEGIN section so it is executed once total instead
of once per input line.
A function definition called once from the BEGIN section isn't
called "once per input line".
Improved version:
{ A[$1] = $2 }
END {
for (i=1; i<=NR; i++)
printf("%s%s",A[i],i<NR ? " " : "\n")
}
Note that the value of NR in END is sort of a gray area, but it works as >expected in GAWK, which is really all we care about.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,030 |
Nodes: | 10 (0 / 10) |
Uptime: | 197:28:38 |
Calls: | 13,340 |
Calls today: | 3 |
Files: | 186,574 |
D/L today: |
2,645 files (810M bytes) |
Messages: | 3,356,968 |