Remove first NULL Character in Flat File

We have a flat file with below data :

^@^@^@^@00000305^@^@^@^@^@^@430^@430^@^@^@^@^@^@^@^@^@09079989530

As we can see ^@ is Null character in this file
I want to remove only the first few null characters before string 00000305
How can we do that, any idea. I want a new file without first few null characters before string 00000305.

Output file format required : 00000305^@^@^@^@^@^@430^@430^@^@^@^@^@^@^@^@^@09079989530

Hello simpltyansh,

Could you please try following and let me know if this helps you.

awk '{sub(/[^[:digit:]]*/,"");print}'

Output will be as follows.

00000305^@^@^@^@^@^@430^@430^@^@^@^@^@^@^@^@^@09079989530

Thanks,
R. Singh

Hi Ravinder,

I used below code and it removed all the NULL characters.

cat file1 | awk '{sub(/[^[:digit:]]*/,"");print}' >> file2

Need urgent help.

Hello simpltyansh,

Please do not use words like "Urgent", it is not encouraged to use these kind of words in forums. Could you please let me know what has NOT worked? Because as per your shown output it worked, please be clear in your questions/asks, show more sample Input_file with code tags and show sample expected output(with all conditions) in code tags too.

Thanks,
R. Singh

NULL characters are always difficult to handle for *nix text tools... try, though,

sed 's/^[[:cntrl:]]*//' file

By definition, UNIX text utility tools work on text files, and text fiies do not contain NUL bytes. Some text utilities on some operating systems can handle NUL bytes even though doing so is not required by the standards. But, of course, you haven't bothered to tell us what operating system you're using.

Are there always three NUL bytes that you want to remove? Or, does the number of NUL bytes vary? If the number varies, you can use something like od to determine the number of leading NUL bytes and then you can use dd to skip over those bytes and copy the rest of the data in the file to another file.

2 Likes