Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'cut' on null?

Tags:

file

null

unix

cut

nul

Unix 'file' command has a -0 option to output a null character after a filename. This is supposedly good for using with 'cut'.

From man file:

-0, --print0
         Output a null character ‘\0’ after the end of the filename. Nice
         to cut(1) the output. This does not affect the separator which is
         still printed.

(Note, on my Linux, the '-F' separator is NOT printed - which makes more sense to me.)

How can you use 'cut' to extract a filename from output of 'file'?

This is what I want to do:

find . "*" -type f | file -n0iNf - | cut -d<null> -f1

where <null> is the NUL character.

Well, that is what I am trying to do, what I want to do is get all file names from a directory tree that have a particular MIME type. I use a grep (not shown).

I want to handle all legal file names and not get stuck on file names with colons, for example, in their name. Hence, NUL would be excellent.

I guess non-cut solutions are fine too, but I hate to give up on a simple idea.

like image 291
philcolbourn Avatar asked Mar 24 '12 01:03

philcolbourn


People also ask

How do you type a null character?

On some keyboards, one can enter a null character by holding down Ctrl and pressing @ (on US layouts just Ctrl + 2 will often work, there is no need for ⇧ Shift to get the @ sign). In documentation, the null character is sometimes represented as a single-em-width symbol containing the letters "NUL".

How do I type null in terminal?

In Linux, any special character can be literally inserted on the terminal by pressing Ctrl + v followed by the actual symbol. null is usually ^@ where ^ stands for Ctrl and @ for whatever combination on your keyboard layout that produces @ .

How do I remove a NULL from a file?

Using the -d switch we delete a character. A backslash followed by three 0's represents the null character. This just deletes these characters and writes the result to a new file.

What Is cut command in Ubuntu?

The cut command is a command-line utility that allows you to cut out sections of a specified file or piped data and print the result to standard output. The command cuts parts of a line by field, delimiter, byte position, and character.


1 Answers

Just specify an empty delimiter:

cut -d '' -f1

(N.B.: The space between the -d and the '' is important, so that the -d and the empty string get passed as separate arguments; if you write -d'', then that will get passed as just -d, and then cut will think you're trying to use -f1 as the delimiter, which it will complain about, with an error message that "the delimiter must be a single character".)

like image 121
ruakh Avatar answered Sep 20 '22 02:09

ruakh