Skip to content

Commit 5b087e9

Browse files
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 <[email protected]> Co-authored-by: Sylvestre Ledru <[email protected]>
1 parent 17d4e4f commit 5b087e9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/uu/cut/src/cut.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,8 @@ fn cut_fields_implicit_out_delim<R: Read, M: Matcher>(
213213
let mut print_delim = false;
214214

215215
if delim_search.peek().is_none() {
216-
if !only_delimited {
216+
if !only_delimited && line[line.len() - 1] == newline_char {
217217
out.write_all(line)?;
218-
if line[line.len() - 1] != newline_char {
219-
out.write_all(&[newline_char])?;
220-
}
221218
}
222219

223220
return Ok(true);

tests/by-util/test_cut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,15 @@ fn test_multiple() {
282282
assert_eq!(result.stderr_str(), "");
283283
}
284284

285+
#[test]
286+
fn test_newline_delimited() {
287+
new_ucmd!()
288+
.args(&["-f", "1", "-d", "\n"])
289+
.pipe_in("a:1\nb:")
290+
.succeeds()
291+
.stdout_only_bytes("a:1\n");
292+
}
293+
285294
#[test]
286295
fn test_multiple_mode_args() {
287296
for args in [

0 commit comments

Comments
 (0)