Skip to content

awk: apply the -F field separator - #206

Open
Harp404 wants to merge 4 commits into
uutils:mainfrom
Harp404:fix-field-separator-option
Open

Harp404 wants to merge 4 commits into
uutils:mainfrom
Harp404:fix-field-separator-option

Conversation

@Harp404

@Harp404 Harp404 commented Nov 30, 2026

Copy link
Copy Markdown

-F was parsed into Args::field_separator but never used, so the option had no effect and fields were always split on blanks:

$ echo a:b:c | awk -F: '{ print $2 }'

$ echo a:b:c | gawk -F: '{ print $2 }'
b

POSIX defines -F sepstring as the same as -v FS=sepstring. This sets FS before any rule runs, and decodes escape sequences the way a string literal does, so -F '\t' splits on tabs. A BEGIN assignment to FS still overrides it.

The change:

  • lexer::unescape decodes the escapes in a command-line value, reusing parse_escape. A trailing lone backslash is kept as is, so -F '\' splits on backslashes like gawk and mawk. This can also be used for -v assignments later; that's still a todo!() in main.rs and I left it alone here.
  • Interpreter::set_field_separator switches the record splitter and stores FS, the same way an assignment to FS in the program does.
  • main.rs calls it before handing the interpreter to the event loop.

I checked -F :, ,, |, ., \t, [0-9], t, \ and ' ' against gawk --posix and mawk, and all now match. -F '' behaves like mawk and gawk without --posix (one field per character).

The new test field_separator_option fails on main and passes with this change. cargo test --workspace passes (273 tests), and cargo fmt and cargo clippy --workspace --all-targets -- -D warnings are clean.

I used Claude Code (Opus 5.5) to help investigate and draft this change; I reproduced the bug and checked the fix myself.

-F was parsed but never used. Set FS from it before any rule runs,
decoding escapes as in a string literal, as POSIX defines -F sepstring
to be the same as -v FS=sepstring.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 14:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/main.rs
// `-F sepstring` is the same as `-v FS=sepstring`, so escapes such as
// `\t` are decoded as in a string literal.
if let Some(fs) = &args.field_separator {
let fs = lexer::unescape(fs.as_encoded_bytes(), args.posix);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mode is Posix for --traditional too, should it be args.posix || args.traditional here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think args.posix alone is right here. That flag only controls the escapes that are GNU extensions (\x, \u), and gawk still decodes those with --traditional:

$ printf 'xAy\n' | gawk --traditional -F '\x41' '{ print $2 }'
y
$ printf 'xAy\n' | gawk --posix -F '\x41' '{ print $2 }'

It is also what the program lexer does: string literals in the program decode \x41 under --traditional. I added that case as a test in 9f09f1e, so it will fail if someone switches it to posix || traditional.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--traditional is weird. It implies POSIX for basically everything but some regex and string parsing shenanigans. It doesn't help that documentation mentions both as "compatibility" modes...

Comment thread tests/by-util/test_awk.rs
("|", "a|b\n", "b\n"),
(".", "a.b\n", "b\n"),
("\\t", "a b\tc\n", "c\n"),
("[0-9]", "a1b2c\n", "b\n"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add the -F '\' and -F '' cases from the description too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 9f09f1e: -F '\' (splits a\b c into a and b c), -F '' (one field per character), and --posix -F '' (the whole record is one field). All three match gawk and mawk.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 02:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! No comments from my end, looks great.

@Alonely0

Alonely0 commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Please fix the conflicts once you have a moment :)

…ption

# Conflicts:
#	tests/by-util/test_awk.rs
Copilot AI balanced review requested due to automatic review settings December 1, 2026 08:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Harp404

Harp404 commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thanks! I merged main in 778a23f to fix the conflict. It was only tests/by-util/test_awk.rs, where field_separator_option and the new else test were added in the same place, so I kept both. cargo test --workspace, fmt and clippy pass. #207 had the same conflict and is updated too (f9bfed0).

Copilot AI balanced review requested due to automatic review settings December 1, 2026 17:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Only two nits from my end.

Comment thread src/main.rs
// `\t` are decoded as in a string literal.
if let Some(fs) = &args.field_separator {
let fs = lexer::unescape(fs.as_encoded_bytes(), args.posix);
intrp.set_field_separator(rt_arena.alloc_slice_copy(&fs));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have to copy this to the arena to make lifetimes work; just take the buffer by-value and create it as a Value::new_string in the interpreter instead of a Value::new_str. Although copying to the runtime arena is not a big deal because it rarely grows after everything is created, it makes our life a bit inconvenient since it boycotts the optimization of growing the last allocation (useful for growing the register array on function calls).

Comment thread lexer/src/lib.rs
let encoding = LocaleEncoding::detect();
let mut out = Vec::with_capacity(bytes.len());
let mut start = 0;
while let Some(rel_i) = memchr(b'\\', &bytes[start..]) {

@Alonely0 Alonely0 Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A big chunk of the FS values that will appear here consist of only one escape, like \t. We could add a fast path that checks whether the first byte is a \, parses the escape sequence, and returns directly if the input is exhausted. In both other cases we fall through to the parsing loop, tho with different start values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants