2

I've searched online for examples, went through the reference manual of the cut command and tested it myself and remain unsure about the -b flag. There aren't any clear examples as to how the cut -b command works. Can someone create an example file and show how it is clearly used?

3

1 Answer 1

4

GNU software is described in info pages, so let’s see what info cut says:

‘-b BYTE-LIST’ ‘--bytes=BYTE-LIST’
Select for printing only the bytes in positions listed in BYTE-LIST. Tabs and backspaces are treated like any other character; they take up 1 byte. If an output delimiter is specified, (see the description of ‘--output-delimiter’), then output that string between ranges of selected bytes.

‘-c CHARACTER-LIST’ ‘--characters=CHARACTER-LIST’
Select for printing only the characters in positions listed in CHARACTER-LIST. The same as ‘-b’ for now, but internationalization will change that. Tabs and backspaces are treated like any other character; they take up 1 character. If an output delimiter is specified, (see the description of ‘--output-delimiter’), then output that string between ranges of selected bytes.

This is from coreutils 8.28 that comes with Ubuntu 18.04 and 18.10, adding multibyte support is in progress and may enable a different behaviour of these two options in the future.

xxd -g1 shows hexdump byte equivalents of characters (after the colon) grouped bytewise as well as its ASCII representation on the left. Take as an example:

$ printf a | xxd -g1
00000000: 61                                               a
$ printf á | xxd -g1
00000000: c3 a1                                            ..

a is a single-byte character with the hex equivalent 61, while á is a two-byte character with the hex equivalent c3 a1. Let’s see how cut from coreutils 8.28 behaves if we try to cut out the first byte or character respectively (0a is just a newline char added by cut and can be ignored):

$ printf ab | cut -b1 | xxd -g1
00000000: 61 0a                                            a.
$ printf ab | cut -c1 | xxd -g1
00000000: 61 0a                                            a.
$ printf áb | cut -b1 | xxd -g1
00000000: c3 0a                                            .. # first byte of á
$ printf áb | cut -b2 | xxd -g1
00000000: a1 0a                                            .. # second byte of á
$ printf áb | cut -c1 | xxd -g1
00000000: c3 0a                                            .. # first byte of á
$ printf áb | cut -c2 | xxd -g1
00000000: a1 0a                                            .. # second byte of á

When it’s implemented, the expected behaviour is:

$ printf ab | cut -b1 | xxd -g1
00000000: 61 0a                                            a.
$ printf ab | cut -c1 | xxd -g1
00000000: 61 0a                                            a.
$ printf áb | cut -b1 | xxd -g1
00000000: c3 0a                                            ..  # first byte of á
$ printf áb | cut -b2 | xxd -g1
00000000: a1 0a                                            ..  # second byte of á
$ printf áb | cut -c1 | xxd -g1
00000000: c3 a1 0a                                         ... # both bytes of á
$ printf áb | cut -c2 | xxd -g1
00000000: 62 0a                                            b.  # second char = b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .