Skip to content
Advertisement

How do I extract everything in a file after the first null character from shell

I have a file that looks like this: some ascii stuffsome more ascii stuffand a little more ascii stuff.

I want to extract everything after the first . So my output after this process would be some more ascii stuffand a little more ascii stuff

How would I go about doing this? This is done within initramfs so my access to commands is somewhat limited. I do have cut, grep, and awk which I’ve been trying to get work, but I’m just not having any luck.

This utils are mostly busybox and sh for the shell

Advertisement

Answer

Easily done, with nothing but shell builtins (well, cat isn’t a builtin, but you can substitute it with the actual intended consumer of your stream):

{ IFS= read -r -d '' _; cat; } <yourfile

read -d '' reads everything, one byte at a time, up to the first NUL on stdin. What’s left on that stream, thus, is all the content after that NUL.


You can test it as follows:

printf '%s' one two three | { IFS= read -r -d '' _; hexdump -C; }

…which properly emits:

00000000  74 77 6f 00 74 68 72 65  65 00                    |two.three.|
0000000a
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement