From 09848bf78b9c65c3c6a41cae05441cd4f02c8f5c Mon Sep 17 00:00:00 2001 From: Andrei Stan Date: Mon, 8 Jan 2024 19:19:55 +0200 Subject: [PATCH] Print lines without delimiters only when they end with specified line terminator('\n' by default or `\0` if `-s`) Signed-off-by: Andrei Stan --- 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 2a3196d002e..7488020560f 100644 --- a/src/uu/cut/src/cut.rs +++ b/src/uu/cut/src/cut.rs @@ -204,11 +204,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 57e6666d304..e468745f6e9 100644 --- a/tests/by-util/test_cut.rs +++ b/tests/by-util/test_cut.rs @@ -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"); +}