uutils/sed

Add `R` command (read one line from file)

Open

#394 创建于 2026年4月30日

在 GitHub 查看
 (0 评论) (0 反应) (0 负责人)Rust (23 fork)github user discovery
enhancementgood first issue

仓库指标

Star
 (91 star)
PR 合并指标
 (PR 指标待抓取)

描述

Bug

The GNU R command (read one line from a file each time it is executed, queue it for output after the current cycle) is not implemented. It is the single-line counterpart to r.

Reproduction

$ printf "x\ny\n" > a; printf "1\n2\n" > b
$ /usr/bin/sed -e '1Rb' -e '2Rb' a
x
1
y
2

$ ./target/release/sed -e '1Rb' a
sed: <script argument 1>:1:2: error: invalid command code `R'

What it should do

From the GNU manual:

R FILENAME Queue a line of FILENAME to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read. Note that if FILENAME cannot be read, or if its end is reached, no line is appended, without any error indication.

Each execution reads exactly one line (until newline) and queues it. Reading a non-existent file is silently ignored, and reaching EOF on subsequent calls is silently ignored.

Suspected place to add it

src/sed/compiler.rs:1276get_cmd_spec. There is already an r command using compile_read_file_command at line 1333. Add:

'R' => Ok(CommandSpec {
    n_addr: 2,
    handler: compile_read_file_command,   // same handler, different exec semantics
}),

Then in processor.rs, the execution side needs to remember a per-R file handle and read one line per invocation. The existing r command slurps the whole file at the end of the cycle; R instead does per-cycle line reads.

Affected GNU testsuite tests

cmd-R.

贡献者指南