Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

File:

O000000667520994000000074720121112000000N^@^@^@ 

I used the below command but it doesn't work.

grep "^@^@^@" *
share|improve this question

migrated from stackoverflow.com Nov 18 '12 at 12:02

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers 5

You can grep for any characters including control/non-printable characters in perl-regexp mode (-P) by its hex code:

grep -P '\x00' ...
share|improve this answer
2  
You might want to add -a option, otherwise grep thinks it is binary data and won't display the matching lines. –  mpy Jun 26 '13 at 15:19
add comment

^@ is not a carat ^ and at-sign @, it's one character. It's how some programs display the NUL character—ASCII value 0, also known as \0 in C.

Here I've created a file with a NUL byte in it. Notice that I use cat -v to show non-printing characters.

$ cat -v blah
hello
null^@
hi
$ hexdump -C blah
00000000  68 65 6c 6c 6f 0a 6e 75  6c 6c 00 0a 68 69 0a     |hello.null..hi.|
0000000f

Grep has trouble finding NULs since they're used to terminate strings in C. Sed, however, can do the job:

$ sed -n '/\x0/p' blah
null
$ sed -n '/\x0/p' blah | cat -v
null^@

In vi, in insert mode press Ctrl-V, Ctrl-Shift-@ to insert a null byte.

share|improve this answer
add comment

Character ^@ is the NUL char, so I'm afraid that it cannot be grepped directly.

Your best option would be probably to write a simple program that searches for this sequence of bytes.

Alternatively you may try to convert it into some form of hexadecimal dump (od, xxd or so) and grep into the output of it. But frankly speaking, it would be tricky to get it right.

share|improve this answer
add comment

^@ is the NUL character. What do you want to do with the lines of your files containing it ?

You could have a look at Identifying and removing null characters in UNIX which deals with a similar issue.

share|improve this answer
add comment

In bash you can add special characters when prefixed with C-q or C-v. So you can, for example

grep 'Ctrl-vCtrl-a' file.txt

The search string should be read as control key + character v, followed by control key + character a, which searches for ASCII value SOH (01). Unfortunately this doesn't work for the NUL character.

share|improve this answer
    
Presumably you don't actually mean that such a character sequence should be written out literally, but instead entered on the keyboard logically? –  Lightness Races in Orbit Nov 17 '12 at 22:10
    
Yes, of course. This is control key held down, press v, then hold down Control key, press a. –  Olaf Dietsche Nov 17 '12 at 22:12
    
I think that's unclear in your answer. –  Lightness Races in Orbit Nov 17 '12 at 22:13
    
@LightnessRacesinOrbit Thanks for the hint. I tried to clarify in the answer. –  Olaf Dietsche Nov 17 '12 at 22:15
    
@JohnKugelman Thanks for the edit. Seems I should have looked into the help more closely. –  Olaf Dietsche Nov 17 '12 at 22:18
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.