Issue
I paste grep manual on arguments -z
and -Z
.
-z, --null-data
Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline. Like the -Z or --null
option, this option can be used with commands like sort -z to process arbitrary file names.
-Z, --null
Output a zero byte (the ASCII NUL character) instead of the character that normally follows a file name. For example, grep -lZ outputs a zero byte after
each file name instead of the usual newline. This option makes the output unambiguous, even in the presence of file names containing unusual characters
like newlines. This option can be used with commands like find -print0, perl -0, sort -z, and xargs -0 to process arbitrary file names, even those that
contain newline characters.
Create a test file:
vim "/tmp/target/it is a test.txt"
test
For -Z
,it output a zero byte 00
at the end of file.
grep -rlZ 'test' /tmp/target |xxd
00000000: 2f74 6d70 2f74 6172 6765 742f 6974 2069 /tmp/target/it i
00000010: 7320 6120 7465 7374 2e74 7874 00 s a test.txt.
For -z
,it Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
.
grep -rlz 'test' /tmp/target |xxd
00000000: 2f74 6d70 2f74 6172 6765 742f 6974 2069 /tmp/target/it i
00000010: 7320 6120 7465 7374 2e74 7874 0a s a test.txt.
Why -z
add 0a
instead of 00
?What does output
mean in Treat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
?
Solution
Why -z add
0a
instead of00
?What doesoutput
mean inTreat input and output data as sequences of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline.
?
The -z
option is about the data that grep
is matching against the given pattern. It has two effects:
The data will be interpreted as having null-terminated lines instead of newline-terminated lines.
When
grep
echos input data to its output (normally a line matching the pattern, but if-v
is in effect then a line not matching the pattern) then it terminates that line with a null character, thus preserving that characteristic of the input. This is what "output" means in the documentation for the-z
option.
Neither of those is relevant to your particular data and grep -rlz
command.
In the first place, the contents of file /tmp/target/it is a test.txt
is just test
-- no null characters in sight. grep
therefore treats the entire contents of the file as one line, though that's no different than if -z
were not in effect, there being no newline either.
In the second place, the -l
option is in effect, so instead of printing any matched lines (with null terminators), grep
prints the name of the file. It appends a newline because that is its default, and you have not overridden it -- filename printing is what the -Z
option is about, not the -z
option.
Note also that -Z
is effective on filename printing even when -l
is not in effect. When -r
is in effect or multiple filenames are given on the grep
command line, grep
will normally precede each line of input data it prints (that is, each output line) with the corresponding file name and a colon. When -Z
is in effect, it instead precedes each line with the file name and a null character.
Answered By - John Bollinger Answer Checked By - Katrina (WPSolving Volunteer)