0

I found this behavior of coreutils utility cut a bit weird

echo "
one,line1
two,line2
three
four,line4
" | cut -d ',' -f1

, got expected result:

one
two
three
four

But:

echo "
one,line1
two,line2
three
four,line4
" | cut -d ',' -f2

, gives outputs:

line1
line2
three
line4

I would expect 3rd line to be empty. Are my expectations wrong?

1 Answer 1

0

No, this is not a bug. From the manpage for the -f option:

Select only these fields; also print any line that contains no delimiter character, unless the -s option is specified

Since your third line has no delimiter, then it prints the whole line.

If you add -s, you will get this output:

line1
line2
line4

If you add a delimiter to your third line at the end, like this

echo "
one,line1
two,line2
three,
four,line4
" | cut -d ',' -f2

Then you will get this:

line1
line2

line4
Sign up to request clarification or add additional context in comments.

1 Comment

Fair point. Reading documentation always helps. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.