From 5b087e96246f1d10c471f9219fa932bacb91ae83 Mon Sep 17 00:00:00 2001 From: andreistan26 <48967297+andreistan26@users.noreply.github.com> Date: Wed, 4 Dec 2024 01:04:55 +0200 Subject: [PATCH] Fix `cut` when lines dont end with specified delim (#5844) Print lines without delimiters only when they end with specified line terminator('\n' by default or `\0` if `-s`) Signed-off-by: Andrei Stan Co-authored-by: Sylvestre Ledru --- src/uu/cut/src/cut.rs | 5 +---- tests/by-util/test_cut.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/uu/cut/src/cut.rs b/src/uu/cut/src/cut.rs index cd6eb22d3..25bb73330 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -213,11 +213,8 @@ fn cut_fields_implicit_out_delim( 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); diff --git a/tests/by-util/test_cut.rs b/tests/by-util/test_cut.rs index 86d3ddf0f..6b376b0ca 100644 --- a/tests/by-util/test_cut.rs +++ b/tests/by-util/test_cut.rs @@ -282,6 +282,15 @@ fn test_multiple() { 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"); +} + #[test] fn test_multiple_mode_args() { for args in [