View Issue Details

IDProjectCategoryView StatusLast Update
00018201003.1(2016/18)/Issue7+TC2Shell and Utilitiespublic2024-05-13 15:08
Reportermortoneccc Assigned To 
PrioritynormalSeverityCommentTypeEnhancement Request
Status ResolvedResolutionAccepted As Marked 
NameEd Morton
Organization
User Reference
Sectionawk
Page Number0
Line Number0
Interp Status---
Final Accepted Text0001820:0006779
Summary0001820: The impact of changing NF should be stated
DescriptionIn most modern awks changing the value of NF has these consequences:

1) making it greater than it currently is will add empty fields up to the value of NF, e.g. if NF is currently 5 then doing `NF += 2` will add fields $6 and $7 with empty content.

2) making it less than it currently is will remove fields from the end down to the value of NF, e.g. if NF is currently 5 then doing `NF -= 2` will delete the fields $4 and $5.

The alternative to "1" is that we set $(NF+2) = "" and get the same effect but there is no good alternative to "2". To be POSIX compliant today without relying on a specific awk's implementation of undefined behavior we either have to do:

    split($0,tmp)
    $0 = ""
    for ( i=1; i<=(NF-2); i++ ) {
        $i = tmp[i]
    }

or:

    sub(/([[:space:]]*[^[:space:]]+){2}[[:space:]]*$/,"")

or similar, both with caveats regarding the values of FS and OFS.
Desired ActionDefine the impact of assigning a value to NF similar to the way that assigning a value to $(NF+n) is defined in the spec today:

---
assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Each field variable shall have a string value or an uninitialized value when created.
---

so we would have:
---
assigning NF a value greater than it has (for example, NF += 2) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Each field variable shall have a string value or an uninitialized value when created.
-
assigning NF a value less than it has (for example, NF -= 2) shall decrease the value of NF; remove fields from the end of the record down to the new value of NF; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS.
-
assigning NF the same value that it already has (for example, NF = NF) shall cause the value of $0 to be recomputed, with the fields being separated by the value of OFS.
---
Tagsissue9

Activities

geoffclare

2024-05-13 15:07

manager   bugnote:0006779

On Issue 8 draft 4.1 page 2613 line 85507, change:
Field variables shall be designated by a '$' followed by a number or numerical expression. The effect of the field number expression evaluating to anything other than a non-negative integer is unspecified; uninitialized variables or string values need not be converted to numeric values in this context. New field variables can be created by assigning a value to them. References to nonexistent fields (that is, fields after $NF), shall evaluate to the uninitialized value. Such references shall not create new fields. However, assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Each field variable shall have a string value or an uninitialized value when created. Field variables shall have the uninitialized value when created from $0 using FS and the variable does not contain any characters. If appropriate, the field variable shall be considered a numeric string (see Expressions in awk, on page 2608).

to:
Field variables shall be designated by a '$' followed by a number or numerical expression. The effect of the field number expression evaluating to anything other than a non-negative integer is unspecified; uninitialized variables or string values need not be converted to numeric values in this context. Each field variable shall have a string value or an uninitialized value when created. Field variables shall have the uninitialized value when created from $0 using FS and the variable does not contain any characters. If appropriate, the field variable shall be considered a numeric string (see Expressions in awk, on page 2608).

New field variables can be created by assigning a value to them. References to nonexistent fields (that is, fields after $NF), shall evaluate to the uninitialized value. Such references shall not create new fields. However, assigning to a nonexistent field (for example, $(NF+2)=5) shall increase the value of NF; create any intervening fields with the uninitialized value; and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Directly increasing the value of NF (for example, NF += 2) shall have the same effect as assigning an empty string to $NF (where NF has its new value). Decreasing the value of NF to a non-negative value shall remove all fields after $NF and cause the value of $0 to be recomputed, with the fields being separated by the value of OFS. Assigning to NF without changing its value (for example, NF = NF) shall cause the value of $0 to be recomputed, with the fields being separated by the value of OFS.

Issue History

Date Modified Username Field Change
2024-03-14 11:36 mortoneccc New Issue
2024-03-14 11:36 mortoneccc Name => Ed Morton
2024-03-14 11:36 mortoneccc Section => awk
2024-03-14 11:36 mortoneccc Page Number => 0
2024-03-14 11:36 mortoneccc Line Number => 0
2024-05-13 15:07 geoffclare Note Added: 0006779
2024-05-13 15:08 geoffclare Interp Status => ---
2024-05-13 15:08 geoffclare Final Accepted Text => 0001820:0006779
2024-05-13 15:08 geoffclare Status New => Resolved
2024-05-13 15:08 geoffclare Resolution Open => Accepted As Marked
2024-05-13 15:09 geoffclare Tag Attached: issue9