Skip to content

Commit

Permalink
Print lines without delimiters only when they end with specified line…
Browse files Browse the repository at this point in the history
… terminator('\n' by default or `\0` if `-s`)

Signed-off-by: Andrei Stan <[email protected]>
  • Loading branch information
andreistan26 committed Jan 15, 2024
1 parent 076b905 commit 09848bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/uu/cut/src/cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,8 @@ fn cut_fields_implicit_out_delim<R: Read, M: Matcher>(
let mut print_delim = false;

if delim_search.peek().is_none() {
if !only_delimited {
if !only_delimited && line[line.len() - 1] == newline_char {
out.write_all(line)?;
if line[line.len() - 1] != newline_char {
out.write_all(&[newline_char])?;
}
}

return Ok(true);
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_cut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,12 @@ fn test_multiple() {
assert_eq!(result.stdout_str(), "b\n");
assert_eq!(result.stderr_str(), "");
}

#[test]
fn test_newline_delimited() {
new_ucmd!()
.args(&["-f", "1", "-d", "\n"])
.pipe_in("a:1\nb:")
.succeeds()
.stdout_only_bytes("a:1\n");
}

0 comments on commit 09848bf

Please sign in to comment.