I am new to Awk programmin.
Given a text table with the following sample entry:
[ 8] SSID[ [HOME]] BSSID[04:9F:xx:xx:xx:xx] channel[ 6] frequency[2437] numsta[1] rssi[-63] noise[-75] beacon[98] cap[1411]
dtim[0] rate[450] enc[Group-AES-CCMP CCMP PSK2 ]
How do you use Awk to quickly & easily break it into:
bssid="04:9F:xx:xx:xx:xx";
ssid[bssid]="[HOME]";
channel[bssid]="6";
frequency[bssid]="2437";
....
rate[bssid]="450;
enc[bssid]="Group-AES-CCMP CCMP PSK2";
I am new to Awk programming.
Given a text table with the following sample entry:
[ 8] SSID[ [HOME]] BSSID[04:9F:xx:xx:xx:xx] channel[ 6] frequency[2437] numsta[1] rssi[-63] noise[-75] beacon[98] cap[1411]
dtim[0] rate[450] enc[Group-AES-CCMP CCMP PSK2 ]
How do you use Awk to quickly & easily break it into:
bssid="04:9F:xx:xx:xx:xx";
ssid[bssid]="[HOME]";
channel[bssid]="6";
frequency[bssid]="2437";
....
rate[bssid]="450;
enc[bssid]="Group-AES-CCMP CCMP PSK2";
On 1/3/2024 10:52 pm, Janis Papanagnou wrote:
BEGIN { FS="] " }
{ for (i=1; i<=NF; i++)
print $i
}
Use of `NF` in awk command - Stack Overflow
the effect of setting `NF` is
undefined behavior per POSIX and so will do different things in
different awk variants and even in 1 awk variant can behave differently >depending on whether you're setting it to a higher or lower than
original value
arnold@freefriends.org (Aharon Robbins) writes:
In article <usqkgn$he7u$2@dont-email.me>,
Ed Morton <mortonspam@gmail.com> wrote:
the effect of setting `NF` is
undefined behavior per POSIX and so will do different things in
different awk variants and even in 1 awk variant can behave differently >>>depending on whether you're setting it to a higher or lower than
original value
This is not true. The effect of setting NF was well defined
by the original awk book and also in POSIX.
Decreasing NF throws away fields. Increasing NF adds the
intervening fields with the null string as their values
and rebuilds the record.
I don't see that in the POSIX specification.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
"""
NF
The number of fields in the current record. Inside a BEGIN action,
the use of NF is undefined unless a getline function without a var
argument is executed previously. Inside an END action, NF shall
retain the value it had for the last record read, unless a
subsequent, redirected, getline function without a var argument is
performed prior to entering the END action.
Kaz Kylheku <433-929-6894@kylheku.com> writes:
On 2024-03-13, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
arnold@freefriends.org (Aharon Robbins) writes:
In article <usqkgn$he7u$2@dont-email.me>,
Ed Morton <mortonspam@gmail.com> wrote:
the effect of setting `NF` is
undefined behavior per POSIX and so will do different things in >>>>>different awk variants and even in 1 awk variant can behave differently >>>>>depending on whether you're setting it to a higher or lower than >>>>>original value
This is not true. The effect of setting NF was well defined
by the original awk book and also in POSIX.
Decreasing NF throws away fields. Increasing NF adds the
intervening fields with the null string as their values
and rebuilds the record.
I don't see that in the POSIX specification.
The key is this:
References to nonexistent fields (that is, fields after $NF), shall
evaluate to the uninitialized value.
NF is assignable, and fields after $NF do not exist. Thus if we
have four fields and set NF = 3, then $4 doesn't exist.
That describes what happens if NF is modified by assignment, but I don't
see that it implies that such an assignment is allowed.
But I can imagine a hypothetical awk-like language in which assigning to
NF has undefined behavior. My question is, how does the POSIX
specification not describe that language?
On the other hand, it also implies that `foo = 42` is valid where `foo`
is the name of a user-defined function (gawk disallows it).
That describes what happens if NF is modified by assignment, but I don't
see that it implies that such an assignment is allowed.
On 3/13/2024 4:49 PM, Kaz Kylheku wrote:
On 2024-03-13, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Kaz Kylheku <433-929-6894@kylheku.com> writes:
On 2024-03-13, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
arnold@freefriends.org (Aharon Robbins) writes:
In article <usqkgn$he7u$2@dont-email.me>,
Ed Morton <mortonspam@gmail.com> wrote:
the effect of setting `NF` is
undefined behavior per POSIX and so will do different things in
different awk variants and even in 1 awk variant can behave differently >>>>>>> depending on whether you're setting it to a higher or lower than >>>>>>> original value
This is not true. The effect of setting NF was well defined
by the original awk book and also in POSIX.
Decreasing NF throws away fields. Increasing NF adds the
intervening fields with the null string as their values
and rebuilds the record.
I don't see that in the POSIX specification.
The key is this:
References to nonexistent fields (that is, fields after $NF), shall >>>> evaluate to the uninitialized value.
NF is assignable, and fields after $NF do not exist. Thus if we
have four fields and set NF = 3, then $4 doesn't exist.
That's a bit like the argument from an old episode of the comedy TV show "Yes, Prime Minister"
in the UK where his aide says (paraphrased) "Some
country has done X, we must go something. War is something, therefore we
must go to war".
Being able to set NF to 3 does not mean you must delete $4.
Why not
delete $1 or $2 instead?
You'd still end up with 3 fields to satisfy the
value of NF.
Do you see something in POSIX that defines the behavior of assigning to
NF?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 498 |
Nodes: | 16 (0 / 16) |
Uptime: | 68:28:32 |
Calls: | 9,814 |
Calls today: | 2 |
Files: | 13,755 |
Messages: | 6,189,407 |